开发者

iOS, fopen hangs / gets stuck

开发者 https://www.devze.com 2023-03-30 02:32 出处:网络
My method to open and read from a file is getting stuck at fopen(). Everyone says it\'s something about FIFO and having to open the other end (it\'s a bloody file, what first in first out?), but no

My method to open and read from a file is getting stuck at fopen().

Everyone says it's something about FIFO and having to open the other end (it's a bloody file, what first in first out?), but no one even gives a hint as to what to do.

const int arraySize = 256;
char tempArray[arraySize];

NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                     NSDocumentDirectory, 
                                                     NSUserDomainMask, YES
                                                     ); 
NSString* docDir = [paths objectAtIndex:0];
NSString* file = [docDi开发者_开发问答r stringByAppendingString:@"/example_01.txt"];

NSLog(file);

//converting to C string
[file getCString:tempArray maxLength:arraySize encoding:NSUTF8StringEncoding];

printf("see %s\n", tempArray);

FILE *lalala;

printf("file pointer befoer: %p", lalala);
lalala = fopen(tempArray, "rb+");

printf("file pointer: %p", lalala);

fseek(lalala, 4, SEEK_SET);

char readin[5];
readin[4] = '\n';
fread(readin, 1, 4, lalala);

fclose(lalala);


Maybe your tempArray is too small and memory gets overwritten.

Try char* tempArray = [file UTF8String]; instead.


Are you considering in using NSFileHandle instead?

Since I am not trying you code, but as you know fopen is a low-level c function, I am not sure if there are some limits on iOS (All of Apps on iOS are running in "sandbox" and have no permissions to read & write anything beside it. )


the cause of the problem :

readin[4] = '\n';

I terminated my string wrong.

In Xcode, it gummed up the printf() that followed to display readin, and appears to have messed up printf("file pointer: %p", lalala); that came before it as well.

Feeling like a nitwit now.

Many thanks to Xuzhe and Hollance for trying to help me out.

0

精彩评论

暂无评论...
验证码 换一张
取 消