在 iOS 设备上编写和访问应用程序文件

发布于 2024-12-15 12:34:21 字数 304 浏览 4 评论 0原文

我有一个程序可以生成自己的 Wireshark pcap 文件(如果您以前没有使用过它,则可以算是网络日志)。我的应用程序使用 ANSI-C 代码来执行此操作。 在 Android 和 Windows 中,我只需使用“fopen”和“fwrite”等将文件写入光盘。

这在 iOS 上如何运作? 我将提供给 fopen 的文件路径是什么?例如,在 Android 中我使用“/sdcard”或类似的,这里是什么? 之后如何从我的 iOS 设备中实际提取该文件?

编辑:我需要明确使用 ANSI-C;所有的编写都是在我的 iOS 应用程序中使用的 C 库中完成的

I have a program which generates its own Wireshark pcap-file (sort of a network log if you havnt used it before). My app uses ANSI-C code to do this.
In Android and Windows I simply use 'fopen' and 'fwrite' etc to write file to disc.

How does this work on iOS?
What is the filepath I will supply to fopen? For example in Android I use "/sdcard" or similiar, what is it here?
How do I actually extract the file afterwards from my iOS device?

EDIT: I need to use ANSI-C explicitly; all the writing is done in C libraries used in my iOS app

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看透却不说透 2024-12-22 12:34:21

您完全可以使用fopen(我在跨平台代码中使用它)。但是,您确实想要使用[documentPath fileSystemRepresentation]

You can totally use fopen (I use it in cross-platform code). You do however want to use [documentPath fileSystemRepresentation].

埋葬我深情 2024-12-22 12:34:21

在 iOS 中,可以使用以下代码找到文档目录路径:

NSArray  *documentDirList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir  = [documentDirList objectAtIndex:0];
NSString *documentPath = [documentDir stringByAppendingPathComponent:@"fileName"];

目标编写方法可能是:

NSData *data = [NSData dataWithBytes:bytes length:length];
[data writeToFile:documentPath atomically:YES];

使用“C”方法(例如 fopen)获取基于字符的字符串:

const char *cDocumentPath = [documentPath cStringUsingEncoding:encoding];

其中 encoding 可能是 NSUTF8StringEncoding 或其他支持的编码。

In iOS the document directory path can be found with this code:

NSArray  *documentDirList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir  = [documentDirList objectAtIndex:0];
NSString *documentPath = [documentDir stringByAppendingPathComponent:@"fileName"];

The Objective writing method might be:

NSData *data = [NSData dataWithBytes:bytes length:length];
[data writeToFile:documentPath atomically:YES];

To use "C" methods such as fopen get the char based string:

const char *cDocumentPath = [documentPath cStringUsingEncoding:encoding];

where encoding might be NSUTF8StringEncoding or another supported encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文