我需要从文件中读取和打印数据。
我编写的程序如下:
数据库文件database.txt包含:
Test1 A
Test2 B
Test3 C
#include<stdio.h>
#include<conio.h>
int main(void)
{
char item[9], status;
FILE *fp;
if( (fp = fopen("D:\\Sample\\database.txt", "r+")) == NULL)
{
printf("No such file\n");
exit(1);
}
if (fp == NULL)
{
printf("Error Reading File\n");
}
while(fscanf(fp,"%s %c",item,&status) == 1)
{
printf("\n%s \t %c", item,status);
}
if(feof(fp))
{
puts("EOF");
}
else
{
puts("CAN NOT READ");
}
getch();
return 0;
}
数据库文件database.txt包含:
Test1 A
Test2 B
Test3 C
运行代码后,输出:
无法读取。
请帮我找出问题所在。
fopen
can fail for many reasons. Printing"No such file"
is not useful when there is a permissions error.man perror
- William Pursell