iOS,fopen 挂起/卡住
我打开和读取文件的方法卡在 fopen() 上。
每个人都说这与 FIFO 和必须打开另一端有关(这是一个该死的文件,什么是先进先出?),但没有人给出关于该怎么做的提示。
const int arraySize = 256;
char tempArray[arraySize];
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask, YES
);
NSString* docDir = [paths objectAtIndex:0];
NSString* file = [docDir 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);
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 = [docDir 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您的 tempArray 太小并且内存被覆盖。
尝试使用
char* tempArray = [file UTF8String];
代替。Maybe your tempArray is too small and memory gets overwritten.
Try
char* tempArray = [file UTF8String];
instead.您是否考虑使用 NSFileHandle 代替?
因为我不是在尝试你的代码,但正如你所知,fopen是一个低级的c函数,我不确定iOS上是否有一些限制(iOS上的所有应用程序都在“沙箱”中运行,并且没有读取权限并在旁边写下任何内容。)
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. )
问题的原因:
我错误地终止了我的字符串。
在 Xcode 中,它混淆了随后显示 readin 的 printf(),并且似乎弄乱了 printf("file point: %p", lalala); 也出现在它之前。
现在感觉自己像个白痴。
非常感谢徐哲和 Hollance 试图帮助我。
the cause of the problem :
I terminated my string wrong.
In Xcode, it gummed up the printf() that followed to display
readin
, and appears to have messed upprintf("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.