Reputation: 191
I am trying to write a simulation program in C where I am appending a file by opening it in append mode. This file is a csv (comma separated values).
I would like to write the headings of my simulation information before I write the actual values so that they don't seem unrelated. Is there an easy way to do this?
For example:
Central Node, System Delay, Bandwidth Requirement
14,240,11
4,285,23
13,300,9
My code looks like this:
void Data_Output(FILE *fp){
struct stat buf;
FILE fd = *fp;
fstat(fd, &buf);
fprintf(stderr,"DEBUG------%d\n",buf.st_size);
}
The output error I get is:
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
What am I doing wrong? Should I be typecasting it in order to make it work?
Upvotes: 2
Views: 293
Reputation: 20842
fstat() works on an integer low level file descriptor, not a FILE * / stream. You need to obtain the descriptor from the FILE * (fp) and use that.
Try:
int fd = fileno(fp);
fstat(fd, &buf);
Upvotes: 1
Reputation: 1410
You can check size of a file. For more info how to get size you can check check this post
Upvotes: 1
Reputation: 11453
You can use the fstat()
function to dump the stats to a buffer, which would contain the size of the file under st_size
.
Upvotes: 0