1

i have a rooted android phone, on which i run native c executables through the adb shell. I am not using neither Java nor the Java NDK-apps, since i want to do low-level work with this app and no form of interaction with the user. While everything runs fine, when i try to write a file (i.e. using std::ofstream of fprintf etc), the file is created but it is empty.

For Apps development i know you need to add a line in the Manifest.XML, so that you have write permissions.

Any suggestions for the native executable?

user1194167
  • 117
  • 9
  • If the file is located on the rootfs or one of the other system partitions, then you must remount the fs as read/write. Other than that, try issuing a sync programaticaly or via adb. – Peter L. Feb 21 '13 at 23:19
  • I understand the remounting, but what do you mean by issuing a sync? – user1194167 Feb 21 '13 at 23:29
  • For example if i type echo "test" > test.txt on adb i can write to the file. For some reason the c program though does not write. – user1194167 Feb 21 '13 at 23:30
  • sync() forces a write. Android will typically cache writes otherwise. – Peter L. Feb 21 '13 at 23:33
  • I tried both the mounting and flushing the file. None of the worked. It has to do with the android permissions probably. – user1194167 Feb 22 '13 at 00:15
  • Does the write() return an error? What is the access mask on the directory? – Peter L. Feb 22 '13 at 00:17
  • OK, when i compile the file as .c (so it compiles using gcc) it is fine, it writes. ok. Now the exact same code (if i put it in a cpp file), so it compiles using g++ it runs but does not write to the file. The code follows `#include #include #include int main() { FILE* file = fopen("/sdcard/hello.txt","w+"); if (file != NULL) { printf("Writing...\n"); fputs("HELLO WORLD!\n", file); fflush(file); fclose(file); } return 0; }` – user1194167 Feb 22 '13 at 06:33
  • Also the if statement, when i compile with C, passes (file != NULL), but when i compile with CPP it does not! (file == NULL). I got the same when i test the good() property of an ofstream for the cpp case. – user1194167 Feb 22 '13 at 06:37
  • OK, a little more info. Printing the errno, i get "Too many open files" Now this does not happen if i compile with C instead of CPP – user1194167 Feb 22 '13 at 07:35

0 Answers0