-1

Basically just the title. I am trying to check wether or not a file is emtpy. The problem is, that this file will get absolutely massive (~1GB+) over the course of a few days, and i feel like

fseek(fp,0,SEEK_END);
if(ftell(fp)==0){//init file}
rewind(fp);
//actually read the file 

Is time expensive, because you have to run through the entire file. I also dont want to use systemcalls, because I normally use Java, and try to work as platform independent as possible (i.e. not having to rewrite it for each platform, but just recompile it). If this is no cocern at all, and fseek()... can handle these filesizes easily, tell me, but i think this is timeconsuming.

PlatinTato
  • 378
  • 2
  • 19
  • 8
    Just read the first byte. If it is `EOF` the file is empty. – Weather Vane Oct 28 '16 at 08:01
  • 1
    `fseek(fp,0,SEEK_END)` is not necessarily slow, even for a big file. `fseek` doesn't need to "run" through the whole file. Think of a file as a CD-Rom rather than a tape. But as a previous comment states: reading the first byte is enough to find out if the file is empty or not. – Jabberwocky Oct 28 '16 at 08:03
  • But on the other hand why do you need to check if the file is empty in first place? Just read the file and if nothing can be read then the file is empty. Tell us more about your project. Is it an [XY problem](http://xyproblem.info/)? – Jabberwocky Oct 28 '16 at 08:04
  • what about checking filesize? I mean isn't there any method to easily return that using fstat etc? – Taha Paksu Oct 28 '16 at 08:14
  • @MichaelWalz I'm trying to write a prime-checker program (yes very original i know), which stores the already known primes in a file. If i want to check if... for example 2^61-1 is a prime, then the file will store about 78000000 primes. Id like to check wether or not i already have primes stored in the first place tho. If not, i return a hardcoded 3 prime-array. Thats why id like to check if the file is empty or not. Im going to test Weather Vanes solution as soon as im at my machine again. I thought this is a very standalone-problem, thats more meta than programdependent. – PlatinTato Oct 28 '16 at 08:17
  • 1
    @TahaPaksu unless im totally missinformed fstat is a systemcall, which im currently trying to avoid – PlatinTato Oct 28 '16 at 08:18
  • 1
    Are you storing the actual primes, or is it a status bit map? If the former you will have to search the whole file (perhaps a binary search), in the latter you can seek directly to where the status bit is. The file might even be *smaller* if you curently store a 64-bit value, because the frequency of primes is more than 1 in 64. – Weather Vane Oct 28 '16 at 08:25
  • @WeatherVane currently im storing the primes. the file looks something like this \n2\n3\n5\n7\n11... – PlatinTato Oct 28 '16 at 08:27
  • 2
    @JacobusConradi you cannot totally avoid system calls anyway because even reading a file will eventually use a system call. – Jabberwocky Oct 28 '16 at 08:56
  • @JacobusConradi *unless im totally missinformed fstat is a systemcall, which im currently trying to avoid* How do you think the C run time library gets the file size in order to make `fseek()` work? If you're trying to be "as platform independent as possible", `fseek()` to the end of a binary file may not be supported. Per **7.21.9.2 The `fseek` function** of [the C Standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf): *A binary stream need not meaningfully support `fseek` calls with a `whence` value of `SEEK_END`* – Andrew Henle Oct 28 '16 at 10:04
  • @AndrewHenle ok i must admit I, first see now how i should have provided more information on the problem, and secondly dont know how fseek works exactly. First I think (not quiet sure on this one, because i dont know how to differentiate them) I am using a text stream. And secondly i thought fseek would run through the file, until it hits EOF. digging through some more documentation however seems to prove me wrong, as SEEK_END appears to be a pointer (not sure here) to the EOF. Any guidance is appreciated :) – PlatinTato Oct 28 '16 at 10:14
  • @AndrewHenle ok i am reviding my last statement. The second part does not make sense. SEEK_END appears to be a constant. So again i feel like `fseek(fp,0,SEEK_END)` again, runs through the entire file – PlatinTato Oct 28 '16 at 10:39

1 Answers1

0

You cannot do anything to or with a file without doing a system call.

But Why don't you want to do a system call? Or do you want to avoid calls to the function named system()?

Using stat() or stat() (depending on wether you already have an open file descriptor or not) probably is the best and fastest way to check, wether the file is empty, or not. And it does not change the state of the file.

Mario Klebsch
  • 361
  • 2
  • 12