c - 如何检查文件是否已经有数据(C语言)?

标签 c file-io

我正在尝试用 C 语言编写一个模拟程序,在其中通过以附加模式打开文件来附加文件。该文件是 csv(逗号分隔值)。

我想在写实际值之前写下模拟信息的标题,这样它们就不会显得无关。有没有简单的方法可以做到这一点?

例如:

Central Node, System Delay, Bandwidth Requirement

14,240,11

4,285,23

13,300,9

我的代码如下所示:

void Data_Output(FILE *fp){
struct stat buf;
FILE fd = *fp;
fstat(fd, &buf);
fprintf(stderr,"DEBUG------%d\n",buf.st_size);
}

我得到的输出错误是:

ff.c: In function ‘Data_Output’:
ff.c:296:2: error: incompatible type for argument 1 of ‘fstat’
fstat(fd, &buf);
^
In file included from /usr/include/stdio.h:29:0,
             from ff.c:1:
/usr/include/sys/stat.h:148:5: note: expected ‘int’ but argument is of type ‘FILE’
 int _EXFUN(fstat,( int __fd, struct stat *__sbuf ));
 ^
Makefile:7: recipe for target 'ff.o' failed
make: *** [ff.o] Error 1

我做错了什么?我应该对其进行类型转换才能使其正常工作吗?

最佳答案

您可以检查文件的大小。 有关如何获取尺寸的更多信息,您可以查看 check this post

关于c - 如何检查文件是否已经有数据(C语言)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22293309/

相关文章:

c - 对所有核心使用 perfsuite

c++ - C/C++ time.h,错误的时间打印出来,时间的比较

c - 打印负数

c - C 中查找文件的简单问题

java - 使用远程 XML 作为文件

c - 读取串口数据

使用 strcpy 连接字符串

字符串数组中的 C 段错误

java - Nginx 配置从 java 代码更改

vba - 我如何一个一个打开所有的excel文件并运行一个宏