1

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

KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

2 Answers2

4

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • sorry if I wasnt clear, I need to use 'fopen' because it is in C libraries used on ios – KaiserJohaan Nov 15 '11 at 15:13
  • Added example to get the char based path, is this what you need? – zaph Nov 15 '11 at 15:19
  • Once you write the files, how can you access them though? Through Itunes somehow? – KaiserJohaan Nov 15 '11 at 16:17
  • Yes, you can access them through iTunes is you specify the capability (`UIFileSharingEnabled`) in the info.plist file. See [File and Document Support](http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS3_2.html) – zaph Nov 15 '11 at 16:21
  • You do not happen to know if there is any troubles using fopen to write to that location? no securty settings needed enabled or such? – KaiserJohaan Nov 15 '11 at 16:37
  • I have not done that but I see no reason there would be a problem. – zaph Nov 15 '11 at 18:14
4

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

David Dunham
  • 8,139
  • 3
  • 28
  • 41