iOS,fopen 挂起/卡住

发布于 2024-12-01 07:00:19 字数 947 浏览 0 评论 0原文

我打开和读取文件的方法卡在 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 技术交流群。

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

发布评论

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

评论(3

陌伤浅笑 2024-12-08 07:00:19

也许您的 tempArray 太小并且内存被覆盖。

尝试使用 char* tempArray = [file UTF8String]; 代替。

Maybe your tempArray is too small and memory gets overwritten.

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

古镇旧梦 2024-12-08 07:00:19

您是否考虑使用 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. )

甩你一脸翔 2024-12-08 07:00:19

问题的原因:

readin[4] = '\n';

我错误地终止了我的字符串。

在 Xcode 中,它混淆了随后显示 readin 的 printf(),并且似乎弄乱了 printf("file point: %p", lalala); 也出现在它之前。

现在感觉自己像个白痴。

非常感谢徐哲和 Hollance 试图帮助我。

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.

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