iphone - 需要在 iOS 项目的 C++ 类中使用 fopen 写入文件

标签 iphone c++ objective-c ios ffmpeg

我有一个使用 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;
}

最佳答案

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

关于iphone - 需要在 iOS 项目的 C++ 类中使用 fopen 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10090618/

相关文章:

iphone - 添加为 subview 时 UITextField 不显示

iphone - IBOutletCollection - 一次连接多个对象

c++ - 为什么那个类型是 double 而不是 float?

objective-c - 如何让程序在登录时自动启动?

iphone - 苹果如何处理拒绝电子邮件?

iphone - 是否有针对 goto 的性能原因?

c++ - 使用模板可以对类型(std::plus,std::minus)之类的对称关系建模吗?

c++ - 为什么 Same<T, U> 不包含 Same<U, T>?

ios - 删除 TableView 中的所有本地通知和单元格

ios - 如何在 animateTransition :? 期间获取 ToViewController 中 subview 的框架