gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:48 25 4
gpt4 key购买 nike

我正在尝试用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com