试图让程序逐字读取文件,直到 EOF,无法获得 while( .... != EOF) 工作

Trying to get program to read a file word by word until the EOF, cannot get while( .... != EOF) to work

提问人:pomalo 提问时间:11/6/2022 最后编辑:Andreas Wenzelpomalo 更新时间:11/6/2022 访问量:62

问:

这是我的代码,我的目的是使用 while 循环来运行代码,直到它读取 Cauchy.txt 文件中的所有单词。它只读取文件中的一些单词。有什么关于我做错了什么的指示吗?

数据是柯西.txt文件如下——

Baron Augustin-Louis Cauchy was a French mathematician reputed as a pioneer of analysis. He was one of the
first to state and prove theorems of calculus rigorously, rejecting the heuristic principle of the generality
of algebra of earlier authors. He almost singlehandedly founded complex analysis and the study of permutation
groups in abstract algebra. A profound mathematician, Cauchy had a great influence over his contemporaries and
successors. His writings range widely in mathematics and mathematical physics.

More concepts and theorems have been named for Cauchy than for any other mathematician (in elasticity alone
there are sixteen concepts and theorems named for Cauchy). Cauchy was a prolific writer; he wrote approximately
eight hundred research articles and five complete textbooks.

我试过了

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<errno.h>

int main()
{
    char MyString[30];

    char fname[] = "Cauchy.txt";   // File name for reading data

    FILE* finp;  // File pointer

    // The next line specifies the complete path
    finp = fopen("C:\\CodingC\\exercise 3\\ReadCauchy\\Cauchy.txt", "r");

    int status = fscanf(finp, "%s", &MyString[0]);
    // Read a few words from the input file
    if ((finp = fopen(fname, "r")) == NULL) {
        fprintf(stderr, "File %s not found\n", fname);
    }
    else {
        fprintf(stdout, "Reading file %s\n", fname);
        do {
            fscanf(finp, "%s", &MyString[0]);
            fprintf(stdout, "%s\n", MyString);
        }
        while (fscanf(finp, "%s", &MyString[0]) != EOF);
    }

    system("pause");
        return 0;
}

这是该程序的内容:

Reading file Cauchy.txt
Baron
Cauchy
a
mathematician
as
pioneer
analysis.
was
of
first
state
prove
of
rigorously,
the
principle
the
of
of
authors.
almost
founded
analysis
the
of
groups
abstract
A
mathematician,
had
great
over
contemporaries
successors.
writings
widely
mathematics
mathematical
More
and
have
named
Cauchy
for
other
(in
alone
are
concepts
theorems
for
Cauchy
a
writer;
wrote
eight
research
and
complete
textbooks.
Press any key to continue . . .
C 文件 if-语句 while-loop eof

评论

1赞 WhozCraig 11/6/2022
while (fscanf(finp, "%s", &MyString[0]) != EOF);你打算把它附在某个地方吗?因为正如所写的那样,这将消耗所有可读的字符序列,直到 EOF,对它们不做任何事情。你实际上有一个循环,它的身体什么都不是dowhile
1赞 Retired Ninja 11/6/2022
您是否打算打开文件,从中读取一次而不检查它是否真的被打开,然后再次打开文件?
0赞 pomalo 11/6/2022
我用do功能稍微修复了文件,现在我有Cauchy.txt文件打印的一部分。有些单词丢失了,不确定现在发生了什么,在此评论之后立即更新我的代码。谢谢你的帮助!:)
1赞 Retired Ninja 11/6/2022
因此,在编辑之后,您将在循环中读取一个值并打印它,然后读取条件中的另一个值并丢弃它。尝试whilewhile (fscanf(finp, "%s", MyString) == 1) { fprintf(stdout, "%s\n", MyString); }
3赞 Retired Ninja 11/6/2022
你仍然跳过每一个其他单词,因为你读了两次,打印了一次。do/while 循环是此作业的错误工具,因为您只想在读取成功时打印。使用 do/while,您无法检查读取是否有效,直到您使用了不正确的数据。我建议重写它,以便您打开文件一次。如果文件指针为 NULL,则处理错误。如果没有,请使用我之前评论中的循环。它的作用是读取一次,然后打印一次,直到读取因任何原因失败。

答:

0赞 pomalo 11/6/2022 #1

这最终奏效了

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<errno.h>

int main()
{
char MyString[30] = { 0 };

char fname[] = "Cauchy.txt";   // File name for reading data

FILE* finp;  // File pointer



// The next line specifies the complete path
finp = fopen("C:\\CodingC\\exercise 3\\ReadCauchy\\Cauchy.txt", "r");


// Read a few words from the input file
if ((finp = fopen(fname, "r")) == NULL) {
    fprintf(stderr, "File %s not found\n", fname);
}
else {
    fprintf(stdout, "Reading file %s\n", fname);
    do {
        
        fprintf(stdout, "%s\  ", MyString);
    }
    while (fscanf(finp, "%s", MyString) != EOF);
}

system("pause");
    return 0;
}

评论

0赞 Oka 11/6/2022
最佳实践:完成后调用 fcloseFILE *
2赞 printf 11/6/2022
虽然这种尝试更好,但它仍然在开始时打印一个不必要的空字符串。就像上面 Retired Ninja 所说的那样,/ loop 在这里不是正确的工具。但是,无论如何,打开文件两次是完全多余的。dowhile
0赞 pomalo 11/6/2022
出于某种原因,当我在 Visual Studio 中运行该文件时,它实际上并没有在开始时打印任何额外的内容(我能注意到吗?),并且打开文件两次,我将搞砸,看看是否可以在不使文件崩溃的情况下删除它
0赞 chux - Reinstate Monica 11/6/2022
do { fprintf(stdout, "%s\ ", MyString); } while (fscanf(finp, "%s", MyString) != EOF); --> while (fscanf(finp, "%29s", MyString) == 1) { fprintf(stdout, "%s ", MyString); }.首先限制输入测试是否成功,而不是一种失败。