我正在尝试读取文件,它告诉我它无法找到该文件。我建立了一个内置的检查器,看看文件是否存在。我的调试文件夹中有数据文件。我是否错误地阅读了文件?我也在使用IDE的代码块。
这是我的函数调用我的文件:
char fileData[3];
int bound = 96;
//file pointer and file info
FILE *ips;
ips = fopen("data.txt", "r");
if (ips == NULL)
printf("Please check file!\n"); //this is the output I get
else {
//for loop to scan through file, and retrive the letters
int i;
for(i=0; i<bound; i++)
fscanf(ips, "%c", &fileData);
addBoggleData(head1, fileData);
}
//closes the file system
close(ips);
}
答案 0 :(得分:1)
您说文件无法打开。
由于fopen()文件名参数没有路径信息。 并且您声明该文件位于调试目录中。
1)执行和文件必须在同一目录
2)在这种情况下,可执行文件和数据文件都必须在调试目录中。
答案 1 :(得分:0)
您将错误的参数传递给fscanf()
,您应该传递i
元素的地址,就像这样
fscanf(ips, "%c", &fileData[i]);
并且为了能够判断数据是否已成功读取,您必须检查fscanf()
的返回值,如
if (fscanf(ips, "%c", &fileData[i]) != 1)
{
warningReadingFailure();
}
此外,fileData
数组太小,你需要使它至少与你打算从文件中读取的字节数一样大,即
int bound = 96;
char fileData[bound];