How to use fgetc to read a file

4.2k views Asked by At

I am sorry the question was not very clear and the code i have so far is shown below and i am just stuck on how i can use the while loop to print the contents of the file.

#include<stdio.h>


int main()
{
FILE *number;


/* Open the file 'numbers' for reading */
number = fopen("numbers.dat", "r");

if (number != NULL) {
/* A bit of space for a line of text */

char lineOfText[100];
while (fgetc(lineOfText, 100, number) != NULL) {
printf("%s\n", lineOfText);
}
   fclose(number);
}
 /* sorry, my question was not clear and to clarify i am trying to 
Print out the contents of the file with one entry per line, my .dat file includes 
1,2,3,4,5,6,7,8,9 and i am trying to print them out in this format
1
2
3
4 and so on...

*/
1

There are 1 answers

3
Keith Nicholas On

To start you off...

FILE * f;
int c;  
f=fopen ("numbers.txt","r");    
while((c = fgetc(f)) != EOF) printf("%c", isdigit(c)? c : ' ');     
fclose (f);