如何使用fgetc读取文件

时间:2013-12-10 20:23:38

标签: c function fgetc

我很抱歉问题不是很清楚,我到目前为止的代码如下所示,我只是坚持如何使用while循环来打印文件的内容。

#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 个答案:

答案 0 :(得分:2)

让你开始......

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