我试图逐行读取文件,但是当调用我的函数时,它会在 while (fgets line with EXC_BAD_ACCESS) 时崩溃。我已将文本文件添加到 Xcode 项目中。
这是有问题的函数:
NSMutableArray *arrayadd(NSString *date){
static const char filename[] = "someText.txt";
FILE *file = fopen ( filename, "r" );
NSString *tmpline;
NSArray *chunks;
NSMutableArray *listtoAdd = [[NSMutableArray alloc] init];
char line [ 512 ];
int a=0;
while ( fgets ( line, sizeof line, file ) != NULL ){
tmpline = [NSString stringWithFormat:@"%s", line];
chunks = [tmpline componentsSeparatedByString: @" - "];
if ((a>0 && a<8) || [chunks objectAtIndex: 0] == date){
[listtoAdd addObject:[chunks objectAtIndex:1]];
a=a+1;
if (a==8){
break;
}
}
}
return listtoAdd;
}
最佳答案
fgets
本身看起来不错,但我注意到您没有检查 fopen
的返回值,这可能意味着 file
是设置为NULL
。
您放置文件的位置可能不是环境运行代码的位置。
关于ios - 在 iOS 应用程序中的 fgets 上获取 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21505564/