Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中使用fscanf()读取文件_C_File_Readfile_Scanf - Fatal编程技术网

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

在C中使用fscanf()读取文件,c,file,readfile,scanf,C,File,Readfile,Scanf,我需要从文件中读取和打印数据。 我写的程序如下所示 #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

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

#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;  
}  
#包括
#包括
内部主(空)
{
字符项[9],状态;
文件*fp;
如果((fp=fopen(“D:\\Sample\\database.txt”,“r+”)==NULL)
{
printf(“无此类文件”);
出口(1);
}  
如果(fp==NULL)
{
printf(“读取文件时出错”);
}
而(fscanf(fp、%s%c)、项目和状态)==1)
{  
printf(“\n%s\t%c”,项目,状态);
}  
if(feof(fp))
{            
卖出期权(“EOF”);
}  
其他的
{  
puts(“无法读取”);
}  
getch();
返回0;
}  
database.txt文件包含
测试1A
测试2 B
测试3 C

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

不识字


请帮我找出问题。

fscanf
将处理2个参数,因此返回2个。您的while语句将为false,因此从不显示已读取的内容,另外,如果它只读取了1行(如果不在EOF),则会导致您看到的内容

首先,您要测试两次
fp
。所以
printf(“读取文件时出错”)永远不会执行

然后,
fscanf
的输出应该等于
2
,因为您正在读取两个值。

在代码中:

while(fscanf(fp,"%s %c",item,&status) == 1)  
为什么是1而不是2?scanf函数返回读取的对象数。

scanf()
和friends返回成功匹配的输入项目数。对于您的代码,这将是两个或更少(如果匹配项少于指定的匹配项)。简而言之,使用手册页面时要更加小心:

#include <stdio.h>
#include <errno.h>
#include <stdbool.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);
    }

    while (true) {
        int ret = fscanf(fp, "%s %c", item, &status);
        if(ret == 2)
            printf("\n%s \t %c", item, status);
        else if(errno != 0) {
            perror("scanf:");
            break;
        } else if(ret == EOF) {
            break;
        } else {
            printf("No match.\n");
        }
    }
    printf("\n");
    if(feof(fp)) {
        puts("EOF");
    }
    return 0;
}
#包括
#包括
#包括
内部主(空)
{
字符项[9],状态;
文件*fp;
如果((fp=fopen(“D:\\Sample\\database.txt”,“r+”)==NULL){
printf(“无此类文件”);
出口(1);
}
while(true){
int ret=fscanf(fp、%s%c)、项目和状态);
如果(ret==2)
printf(“\n%s\t%c”,项目,状态);
else if(errno!=0){
佩罗尔(“scanf:”);
打破
}否则如果(ret==EOF){
打破
}否则{
printf(“不匹配。\n”);
}
}
printf(“\n”);
if(feof(fp)){
卖出期权(“EOF”);
}
返回0;
}

谢谢:)//while(42)它是什么意思?它相当于
while(1)
或伪代码
while(true)