需要在 iOS 项目的C++类中使用 fopen 写入文件
Need to write to a file using fopen in a C++ class for iOS project
我有一个使用 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:%dn",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
重新运行适合文件系统编码的字符*,例如可能转换为 UTF-8
相关文章:
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- fopen 在创建大量文件后因 EFBIG "File too large" 而失败
- 在C++中打开的带有"fopen"的mmap文件
- 无法使用 fopen() 加载文件
- 在我可以从fopen打开文件时,CopyFile无法找到该文件
- 使用MMAP与FSTREAM或FOPEN访问二进制文件
- fopen 和 fwrite 从多个线程到同一个文件
- fopen 几分钟后无法打开文件
- 我如何确保Fopen()相对于可执行的目录打开一个文件,而不是我当前的目录
- 在地铁应用程序中使用fopen打开文件
- fopen 无法完全打开文件
- 在VS2015中使用fopen写入文件时发生Acess违规
- 使用文本编辑器打开一个 txt 文件,而它已经被 "fopen()" 打开并正在使用中?
- 使用fopen for file时,matlab在没有转储文件的情况下崩溃
- 使用fopen打开文件,在Windows上给定绝对路径
- 使用 fopen 读取C++文本文件,无需换行转换
- 截断使用 fopen 打开的文件
- 打开多个文件,fopen() 的替代品
- 在C++中使用 fopen 的隐式文件扩展名
- Eclipse 在使用 freopen 或 fopen CPP 时无法读取文件