2

I am using a native library which creates and writes to a binary file(a network logging file) with fopen(). I am using this library on both android and windows. On Android, the file is not deleted & created upon calling fopen(), while it is so on windows. This is troublesome because I want it to be reset everytime I start the program; as it is now, the logger just accumulate data when I run it on android.

I fopen() the file like this;

#ifdef ANDROID
        pcapFP = fopen("/mnt/sdcard/log.pcap","wb+");
#else 
        pcapFP = fopen("log.pcap","wb+");
#endif

Any idea why the Android file dosn't reset?

Thanks

EDIT: It seems that if the process is killed, then the file is reset upon restarting the app however. This is not good enough however, I want to reset it when I want with *fopen()/fclose().*

KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

2 Answers2

1

The Standard specifically says in the description of the fopen function:

7.19.5.3/3
...
w+b or wb+ truncate to zero length or create binary file for update

so, apparently, your Android implementation (compiler and library) is not Standard conformant :(

Try different compilation flags maybe???

pmg
  • 106,608
  • 13
  • 126
  • 198
  • Android's libc (Bionic) is known to be crap, but I'm surprised at this... As far as I could tell stdio was just copied from BSD code. In plenty of other areas though, they've just thrown out the standards and done "whatever some inexperienced programmer thought made sense" in place of the standard behavior... – R.. GitHub STOP HELPING ICE Sep 19 '11 at 14:16
  • Hmm ... maybe [@Ofir's suggestion](http://stackoverflow.com/questions/7472160/android-fopen-does-not-reset-the-file/7472259#7472259) (hélas, now deleted) of using `"wb"` instead of `"wb+"` works for Android/Bionic. I don't have an Android device and can't test it. If that works and you can live without the update mode ... you're home free, Kaiser :) – pmg Sep 19 '11 at 14:28
0

what about deleting the file. u can do it any time, prior to exitting, prior to reading, or as the app starts up. i see u labeled this as c which confuses me a little because java is usually used for android.

File file = new File(ih.getImgPath(id)); boolean deleted = file.delete();

LINK 1

LINK 2

Community
  • 1
  • 1
owen gerig
  • 6,165
  • 6
  • 52
  • 91