0

I've managed to compile android from source and install on my phone. I should probably say from the outset I'm not a c++ developer but I had hoped I would be able to work out how to output the data held in a variable to a file.

After a bit of googling I came up with the following code:

#include <iostream>
#include <fstream>
using namespace std;

ofstream myfile;
myfile.open ("/data/wav.raw");
myfile < mMixBuffer;
myfile.close();

frameworks/base/services/audioflinger/AudioFlinger.cpp: In member function 'virtual bool android::AudioFlinger::MixerThread::threadLoop()':
frameworks/base/services/audioflinger/AudioFlinger.cpp:1769: error: 'ofstream' was not declared in this scope
frameworks/base/services/audioflinger/AudioFlinger.cpp:1769: error: expected ';' before 'myfile'
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'myfile' was not declared in this scope

after this didn't work I tried which again gave compilation errors:

int fd;
fd = ::open("/data/wav.raw",O_WRONLY | O_APPEND);
::write(fd,mMixBuffer,minBufferSize);
::close(fd);

frameworks/base/services/audioflinger/AudioFlinger.cpp: In member function 'virtual bool android::AudioFlinger::MixerThread::threadLoop()':
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: '::open' has not been declared
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'O_WRONLY' was not declared in this scope
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'O_APPEND' was not declared in this scope

If it's not clear from the above I'm trying to append the contents of variable mMixBuffer with size minBufferSize to a file /data/wav.raw. Does anyone have any idea how I can achieve this?

James
  • 1,720
  • 5
  • 29
  • 50
  • It's always helpful, when you get compilation errors, to show what those errors actually are. It looks like you may not be putting this code inside any function, but it's hard to be sure. – Greg Hewgill Dec 13 '12 at 23:08
  • Ok, sorry - I thought it would be likely the code would be so incorrect that the compilation errors would clutter the post more than help. I'll add them now. – James Dec 13 '12 at 23:11
  • Are you sure you have `#include ` for first code sample, and `#include ` for second one? – Mārtiņš Možeiko Dec 13 '12 at 23:20
  • `#include ` is definitely there for the first one - I read online and it said the std libraries are not included in android - there was a suggestion to add `APP_STL := stlport_static` to a makefile but that didn't seem to work. I'm just trying `#include ` - I think it looks promising – James Dec 13 '12 at 23:33
  • It'll compile with ``. It won't actually work unless you're running as root. Make sure you check for errors -- those functions don't throw exceptions. If the value you get back from `open()` is < 0, the error will be in `errno` (tip: `#include `). – fadden Dec 14 '12 at 01:10
  • thanks for the tip. Can you confirm what you mean by 'running as root'? If it's because of the write dir I've changed it to `/sdcard/wav.raw`, will that change things? – James Dec 14 '12 at 09:55

1 Answers1

1

/data/ directory can not be modified. You won't be having the permissions for the same. Try /mnt/sdcard (or) /data/data/.

Suman
  • 4,221
  • 7
  • 44
  • 64
  • thanks. I realised this a few days ago and tried /sdcard but that didnt work - I have yet to look at the error code. I'll give /mnt/sdcard a go – James Dec 18 '12 at 14:11