c - 如何使用fgetc读取文件

标签 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...

*/

最佳答案

让你开始......

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/

相关文章:

algorithm - 任何人都可以改进以下 VBA 的 Fuzzyfind 函数吗?

C++从类中访问函数,接收函数作为参数

c - 在 C 中的行尾后读取问题

c - Sscanf - 使用不同的分隔符进行解析

PHP:如何调用标准库函数

c - Varnish 守护进程未启动 : Resource temporarily unavailable

c - 循环中的 fgetc(stdin) 产生奇怪的行为

c - 输入错误后循环返回,fgetc(stdin) 问题和循环

c - 实现文件描述符

c - 将 C 字符串数组传递给 Fortran (iso_c_binding)