5

我有一个使用 C++ 类和 FFmpeg 的项目,我需要使用 fopen 并将文件写入应用程序沙箱,因此我需要用 C++ 编写的代码相当于:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs_dir = [paths objectAtIndex:0];

这将我进入我的应用程序沙箱,在那里我几乎可以操作我的文件问题是我如何在 C++ 中编写此代码以便我可以在文件上使用 fopen?

这是需要实现的方法:

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;
}
4

1 回答 1

8
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");

消息-fileSystemRepresentation重新运行为文件系统适当编码的 char*,例如可能转换为 UTF-8

于 2012-04-10T14:38:11.993 回答