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

发布于 2024-09-11 22:46:09 字数 700 浏览 3 评论 0原文

我需要从文件中读取并打印数据。
我编写了如下程序,

#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 文件包含
测试1 A
测试2 B
Test3 C

当我运行代码时,它打印

无法阅读。

请帮我找出问题所在。

I need to read and print data from a file.
I wrote the program like below,

#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;  
}  

the database.txt file contains
Test1 A
Test2 B
Test3 C

When I run the code, it prints

CAN NOT READ.

Please help me to find out the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撧情箌佬 2024-09-18 22:46:10

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

fscanf will treat 2 arguments, and thus return 2. Your while statement will be false, hence never displaying what has been read, plus as it has read only 1 line, if is not at EOF, resulting in what you see.

风渺 2024-09-18 22:46:09

首先,您要测试 fp 两次。所以 printf("Error Reading File\n"); 永远不会被执行。

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

First of all, you're testing fp twice. so printf("Error Reading File\n"); never gets executed.

Then, the output of fscanf should be equal to 2 since you're reading two values.

别挽留 2024-09-18 22:46:09

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

#include <stdio.h>
#include <errno.h>
#include <stdbool.h>

int main (void) {
    FILE *fp = fopen ("D:\\Sample\\database.txt", "r+");

    if (!fp) {
        puts ("No such file\n");
        return -1;
    }

    char item[9] = {}, status = 0;
    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 {
            puts ("No or partial match.\n");
        }
    }
    puts ("\n");
    if (feof (fp)) {
        puts ("EOF");
    }
    return 0;
}

scanf() and friends return the number of input items successfully matched. For your code, that would be two or less (in case of less matches than specified). In short, be a little more careful with the manual pages:

#include <stdio.h>
#include <errno.h>
#include <stdbool.h>

int main (void) {
    FILE *fp = fopen ("D:\\Sample\\database.txt", "r+");

    if (!fp) {
        puts ("No such file\n");
        return -1;
    }

    char item[9] = {}, status = 0;
    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 {
            puts ("No or partial match.\n");
        }
    }
    puts ("\n");
    if (feof (fp)) {
        puts ("EOF");
    }
    return 0;
}
浅唱々樱花落 2024-09-18 22:46:09

在你的代码中:

while(fscanf(fp,"%s %c",item,&status) == 1)  

为什么是 1 而不是 2? scanf 函数返回读取的对象数。

In your code:

while(fscanf(fp,"%s %c",item,&status) == 1)  

why 1 and not 2? The scanf functions return the number of objects read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文