» Basic File IO in C by Christopher Hill -TrajectoryLabs.com |
| | Page 5 of 7 | |
| < Previous | | Next > |
|
(Login to remove green text ads)
Read lines using fgets()
Create a text file "more.txt".
Code:
// Read lines using fgets()
#include <stdio.h>
int main()
{
char text[10];
FILE *file_ptr; // 1. declare a FILE pointer
file_ptr = fopen("more.txt", "r"); // 2. open file
if( file_ptr !=NULL ) // 3. test
{
printf("File more.txt opened. \nContents:\n");
while(fgets(text,10,file_ptr) !=NULL) //4. read text
{
printf("%s", text);
}
fclose(file_ptr); // 5. close the file
return 0;
}
else{printf("Unable toopenfile.\n"); return 1; }
}
|
| |
| | Page 5 of 7 | |
| < Previous | | Next > |
|
|