小编Gur*_*uru的帖子

使用C中的fscanf()读取文件

我需要从文件中读取和打印数据.
我写的程序如下,

#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;  
}  
Run Code Online (Sandbox Code Playgroud)

database.txt文件包含
Test1 A
Test2 B
Test3 C.

当我运行代码时,它会打印出来

无法阅读.

请帮我找出问题所在.

c file scanf readfile

15
推荐指数
2
解决办法
24万
查看次数

标签 统计

c ×1

file ×1

readfile ×1

scanf ×1