0

I have ported a very large application to Android. It receives binary data over TCP/IP, and writes it to files which it uses now and then in the application. The directory to which it saves the files is by default set to ./file_cache. It goes wrong here:

fileHandle = fopen(filename,"wb");

filename in this case is "file_i" where i starts from 0 and increments for every file.

The program crashes on writing, and it doesn't have to crash on the first file, sometimes it gets as far as the 10th file. When it crashes the fileHandle ends up as NULL.

The filename is initially in wchar, but is converted before (I print it out in the logger to confirm that it is correct).

What I have tried:

  • I have added all permissions that seem to have anything to do with file read/write external storage.

  • I have tried different locations /sdcard/files and /data/data/<package>/files

Questions:

  1. What is the current directory, i.e. where do the files really end up on the default setting?

  2. What can be causing fopen to return null, which in turn is the cause of the crash?

Very thankful for any ideas.

Jake
  • 660
  • 1
  • 7
  • 18
  • I won't rely on current directory in an Android app - the whole notion of a current directory smells for non-console software. Just use full path instead. Use `Context.getDir()` to get a data folder path for the app. – Seva Alekseyev Oct 22 '12 at 17:01
  • Like I said, I tried other directories as well. Is there a way to get that data folder in NDK? I'm not using Java at all and I very much perfer not to. – Jake Oct 22 '12 at 17:10
  • It's available in `android_app` that your `android_main` gets as a parameter. Specifically, it's `state->activity->internalDataPath`. If you're using NativeActivity, please tag the question with `native-activity` - the environment is markedly different. – Seva Alekseyev Oct 22 '12 at 18:07
  • What is the **crash**? Is the only manifestation of the crash that `fopen()` returns NULL? In this case, id you try to call `fopen()` again? If there were some relevant lines in logcat before `fopen()`, please post them. – Alex Cohn Oct 23 '12 at 21:59
  • Did you ever found the solution? I am experiencing the same problem – PerracoLabs May 12 '13 at 21:02

1 Answers1

0

I found the problem, though it was a general program error. The filename "file_i" was due some complicated code being assigned size 1, which naturally was too small, causing the segmentation fault. So it had nothing to do with fopen. Actually, all segmentation faults I've had have been caused by a fault similar to this so it can be worth really scrutinizing your code whenever you are messing with char/wchar.

Moreover, I use the /data/data/.. folder which works fine, haven't actually tried the sdcard one but can't see why it would not work.

Jake
  • 660
  • 1
  • 7
  • 18