1

I want to use a if statement to check if a file size is greater than a determined number in kb. For example:

if(filesize GTREATER than VARX){
 DO
}else{
 DO
}

I'm new to C yet, so please, can you explain clearly, if I have to use a different function.

Joseph Stine
  • 1,022
  • 1
  • 12
  • 23
ghaschel
  • 1,313
  • 3
  • 20
  • 41
  • You need to get the file size of the file. That has been asked for before on SO, and can be find at: http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c After that, you can compare the size returned against whatever value you want. – StarPilot May 25 '12 at 14:13

1 Answers1

2

If you've already fopen()ed the file, then you can use fstat(). stat() or lstat() can be used on files by name (in a string), but are somewhat less efficient if you do it repeatedly. You'll need to use fileno() to get the file descriptor (int) from the FILE * returned by fopen(). Otherwise, the man pages have reasonable examples.

twalberg
  • 59,951
  • 11
  • 89
  • 84
  • 1
    `fstat`, `stat`, and `fileno` are all POSIX and can work if you're on a system that (at least approximately) supports POSIX functions, but the portable C way to do this is with `fseek` and `ftell`. – R.. GitHub STOP HELPING ICE May 25 '12 at 14:21