提问人:pomalo 提问时间:11/6/2022 最后编辑:Andreas Wenzelpomalo 更新时间:11/6/2022 访问量:62
试图让程序逐字读取文件,直到 EOF,无法获得 while( .... != EOF) 工作
Trying to get program to read a file word by word until the EOF, cannot get while( .... != EOF) to work
问:
这是我的代码,我的目的是使用 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 . . .
答:
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;
}
评论
2赞
printf
11/6/2022
虽然这种尝试更好,但它仍然在开始时打印一个不必要的空字符串。就像上面 Retired Ninja 所说的那样,/ loop 在这里不是正确的工具。但是,无论如何,打开文件两次是完全多余的。do
while
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); }
.首先限制输入测试是否成功,而不是一种失败。
评论
while (fscanf(finp, "%s", &MyString[0]) != EOF);
你打算把它附在某个地方吗?因为正如所写的那样,这将消耗所有可读的字符序列,直到 EOF,对它们不做任何事情。你实际上有一个循环,它的身体什么都不是。do
while
while
while (fscanf(finp, "%s", MyString) == 1) { fprintf(stdout, "%s\n", MyString); }