很抱歉,问题不是很清楚,到目前为止我的代码如下所示,我只是坚持如何使用 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...
*/
最佳答案
让你开始......
FILE * f;
int c;
f=fopen ("numbers.txt","r");
while((c = fgetc(f)) != EOF) printf("%c", isdigit(c)? c : ' ');
fclose (f);
关于c - 如何使用fgetc读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20504453/