views:

57

answers:

2

Hi, is there a way to write files to the iphone file system using only pure c code and some posix like api?

I only found some Objective-C/Cocoa stuff out there and I think that I can't just mix some Objective-C code snippet into the .c file.

A: 

Objective-C is a layer built on top of the C language; so potentially anything that's written in C or anything that's in the Standard C library can be called in your Objective-C code.

For file IO, you can use the standard C file IO:

NSString *filePath = @"YOUR FILE PATH";
FILE *file;
file = fopen([filePath cStringUsingEncoding:NSUTF8StringEncoding], "w");

fprintf(file, "YOUR OUTPUT HERE\n");

fclose(file);
Jeff
Obviously it should be possible to use c code in objective-c. But we want to do it the other way round.
You want to call Objective-C code from C?
Jeff
See my answer on how to call Objective C from C. It requires a separate Objective C .m file and an ANSI C helper function in that file.
hotpaw2
Don't use `-cStringUsingEncoding:`, use `-fileSystemRepresentation`
JeremyP
+2  A: 

Objective C is a pure superset of plain ANSI C. You can just drop any pure C code into any Objective C .m file. (The opposite won't work because the compiler won't know to compile a .c file using the language superset.)

Added: The way to do it the other way around is not to try and directly put Objective C in a C file, but to put the Objective C in a new Objective C .m file with a C wrapper function to call that Objective C method also in that .m file. Then use a C function call in the C file to call from that C file to the Objective C file's C wrapper function, and have that C wrapper function in the Objective C file call the Objective C method.

Most C posix calls work normally on the iPhone, although some are illegal if they try to reach outside the security sandbox (e.g. you may have to use a bit of Objective C to determine which legal file paths the posix fopen() call can use.)

I've taken large C apps and wrapped them up in just enough Objective C to connect them with the Cocoa Touch UI framework. A few pages worth.

hotpaw2
So basically to port a c application to iphone, you have to write a wrapper...
@user299831 : Yes, given that there is no stdin, stdout console (when not debugging), you have to connect to the user's GUI via a UI wrapper. Or have a blank app that appears to do nothing.
hotpaw2