0

I'm trying to use fopen() in a .cpp file as

fp = fopen("/sdcard/inputdump.txt", "wb");

to dump some data into it and later analyse the contents of the file.

The problem is fp is always NULL irrespective of the path mentioned in the fopen(). So is there any other way we can use instead of dumping it into a file?

MSalters
  • 173,980
  • 10
  • 155
  • 350

1 Answers1

0

Using freopen("./path/to/file.txt", "w", stdout); you can redirect standard output to file and save your staff just using cout. As an example

cout << "write this text to file" << endl;

Also ofstream do the same without changing the standard output.

#include <fstream>
using namespace std;
...
ofstream out("./path/to/file.txt", ofstream::out);
out << "write this text to file" << endl;

PS: See this link fopen has not b option.

Bonje Fir
  • 787
  • 8
  • 25
  • where does `"./path/to/file.txt"` point to in the physical file st\ystem? – pskink Nov 13 '17 at 09:51
  • @pskink Look at [this link](https://stackoverflow.com/questions/41283619/getting-absolute-path-on-android). There is no note about what platform(android) you are asking the question text! – Bonje Fir Nov 13 '17 at 10:00
  • there was a tag `android` that was deleted one minute ago – pskink Nov 13 '17 at 10:04
  • @pskink If you ask this Q for a PC app. `.` means the current location of executable file in the `"./path/to/file.txt"`. So simply `./file.txt` means that your file is beside the app executable. – Bonje Fir Nov 13 '17 at 10:05
  • i know that but on android platform there is no notion of "current location" – pskink Nov 13 '17 at 10:05
  • @pskink You must mention it in the question statement or even title. Someone removed the tag since he/she think it is redundant. – Bonje Fir Nov 13 '17 at 10:06
  • @pskink I am not an android expert but I think it gives you a folder for your data which is temporary. you must get its path using some [API](https://stackoverflow.com/questions/7595324/creating-temporary-files-in-android-with-ndk). – Bonje Fir Nov 13 '17 at 10:12
  • i think OP problem is file permissions: (note he is using the absolute file path) but still his code does not work – pskink Nov 13 '17 at 10:16
  • @pskink You need [this](https://stackoverflow.com/questions/4731314/android-ndk-write-file) – Bonje Fir Nov 13 '17 at 10:20