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