1

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Aditya Somani
  • 181
  • 13
  • possible duplicate of [How can I determine if a file is empty?](http://stackoverflow.com/questions/11015485/how-can-i-determine-if-a-file-is-empty) – alecbz Mar 10 '14 at 06:23
  • Read about fseek, fset. – Abhineet Mar 10 '14 at 06:26
  • Presuming you want to know the file's size is non-zero - check out this [how-can-i-get-a-files-size-in-c](http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c) for the `stat` or `fstat` variants. – n0741337 Mar 10 '14 at 06:26
  • You can check file size in C you can get more help to get size [a link](http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c) – Guru Mar 10 '14 at 06:29
  • @alecbenzer I agree. The question you mentioned should answer my queries. Thanks. – Aditya Somani Mar 10 '14 at 06:29
  • If this file is written to by potentially multiple processes it would be a good idea to use advisory locking too :) – Ja͢ck Mar 10 '14 at 06:58
  • possible duplicate of [How do you determine the size of a file in C?](http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c) – Ja͢ck Mar 10 '14 at 06:59
  • I read the links about and I realized the mistake I was making. I'll update the above in order to reflect working code. Thanks everyone! – Aditya Somani Mar 10 '14 at 07:01

3 Answers3

1

You can check size of a file. For more info how to get size you can check check this post

Community
  • 1
  • 1
Guru
  • 1,303
  • 18
  • 32
0

You can use the fstat() function to dump the stats to a buffer, which would contain the size of the file under st_size.

Arjun Sreedharan
  • 11,003
  • 2
  • 26
  • 34
  • [`fstat`](http://codewiki.wikidot.com/c:system-calls:fstat) is a Posix-standard library, not a C-standard library. Other than using `fseek` and `ftell`, I don't think there *is* a standard C way - and using `ftell` has provisos. Even so, it amazes me the way some developers assume the C standard implies the Posix standard - if I implied the C standard included Windows libraries such as [`GetFileSize`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364955%28v=vs.85%29.aspx), you can bet they'd be crying foul. –  Mar 10 '14 at 06:43
  • @Steve314 `ftell` can be used only if file is opened in binary mode. – Arjun Sreedharan Mar 10 '14 at 06:47
  • yes, that's one of the provisos (and mentioned in the link Guru provided already). Another that may not have been mentioned is that the file size may be changing (unless you have exclusive access), and yet another is that some "files" (such as standard output) don't have a size. –  Mar 10 '14 at 06:49
  • `fstat` takes a file descriptor as its argument. You can use `fileno()` to get the descriptor from the file pointer. – Arjun Sreedharan Mar 10 '14 at 07:03
0

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);
codenheim
  • 20,467
  • 1
  • 59
  • 80