Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to write to a file using fopen in a C++ class for iOS project

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;
}
like image 680
Chuck Mc Duran Avatar asked Apr 10 '12 14:04

Chuck Mc Duran


People also ask

How do you write to a file in Objective C?

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];

Can you create a file with fopen?

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.

How do I create a .MM file in Xcode?

You just need to change the extension to . mm. There is no option to create a file with . mm extension in Xcode.

What is .a file in Objective C?

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.


1 Answers

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

like image 136
JeremyP Avatar answered Nov 14 '22 23:11

JeremyP