I have a project that uses C++ classes and FFmpeg, I need to use fopen and write a file to the app sandbox, so the code that I need to write in C++ is the equivalent of:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs_dir = [paths objectAtIndex:0];
This would me to my app sandbox, where I can pretty much manipulate my files The question is how do I go about writing this code in C++ so that I can use fopen on a file?
This is the method that needs implementation:
int testGrabAndWrite(const char* streamURL, bool decode, const char* filename)
{
FILE *outfile;
int ret;
int counter = 0;
uint8_t *data; // Pointer to the received audio mem
int size; // Size of the received audio buffer
outfile = fopen(filename, "w");
if (outfile == NULL)
exit(1);
// Open the RTP stream
if ((ret = openStream(streamURL, decode)) < 0)
return ret;
// Print out info about the stream found
int tmpSampleRate, tmpBitRate, tmpChannels;
ret = getStreamInfo(tmpSampleRate, tmpBitRate, tmpChannels);
printf("\nSample rate:%d Bit rateL%d Channels:%d\n",tmpSampleRate,tmpBitRate, tmpChannels);
// Grab some sample data and write it to file.
while (counter < 500)
{
ret = getStreamData(data, size);
fwrite(data, 1, size, outfile); // Write RTP packets, i.e. mp3, to file.
printf("Wrote packet %d with size %d which returned %d. ", ++counter, size, ret);
}
fclose(outfile);
closeStream();
return ret;
}
Having stored the contents of a file in an NSData object, that data may subsequently be written out to a new file using the createFileAtPath method: databuffer = [filemgr contentsAtPath: @"/tmp/myfile. txt" ]; [filemgr createFileAtPath: @"/tmp/newfile. txt" contents: databuffer attributes: nil];
The fopen function creates the file if it does not exist. r+b or rb+ Open a binary file for both reading and writing. The file must exist.
You just need to change the extension to . mm. There is no option to create a file with . mm extension in Xcode.
A . a file is a compiled static library file (aka archive). It does not contain any source code, only object code for one or more architectures.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs_dir = [paths objectAtIndex:0];
NSString* aFile = [docs_dir stringByAppendingPathComponent: @"somedocthatdefinitelyexists.doc"];
FILE* fp = fopen([aFile fileSystemRepresentation], "r");
The message -fileSystemRepresentation
retruns a char* suitably encoded for the file system e.g. probably converted to UTF-8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With