strcmp 和 fscanf 带 While 循环遍历文件
strcmp and fscanf with a While Loop through a file
我在处理特定代码行时遇到了问题,该代码行给出了
的错误
error: invalid conversion from ‘int’
to ‘const char*’
error: initializing argument 1 of ‘int strcmp(const char*, const char*)’
有谁知道为什么?这是有问题的代码行。
while (strcmp(fscanf(fr, "%s", words), "DONE") != 0)
本质上,我的代码扫描文件(执行某些操作)直到它到达关键字“DONE”(不带引号),然后退出文件。我是初学者 C 程序员,所以请原谅代码中的任何 inaccuracies/inefficiencies。
完整代码如下。
#include <stdio.h>
#include <string.h>
FILE *fr;
struct player {
char name[50];
float DOC;
};
struct player players[50];
int main() {
fr = fopen ("playerinfo.txt", "r");
if (ftell(fr) == 0) {
fclose(fr);
printf("PLAYER FILE IS EMPTY");
return 0;
}
char words[50];
while (strcmp(fscanf(fr, "%s", words),"DONE") != 0) {
float pts;
fscanf(fr, "%f", pts);
float asts;
fscanf(fr, "%f", asts);
float mins;
fscanf(fr, "%f", mins);
struct player *aPlayer;
float theDOC = (pts + asts) / mins;
strcpy(aPlayer->name, words);
aPlayer->DOC = theDOC;
}
fclose(fr);
return 0;
}
在您的代码中,
strcmp(fscanf(fr, "%s", words),"DONE")
并不像你想象的那样。 fscanf()
does not return a pointer to the scanned string, rather, it returns a count (int
type). Your compiler warned you. Read the man page before you proceed.
这种不当使用会导致警告。
也就是说,您必须检查 scanf()
系列函数是否成功,否则,您很有可能最终使用不确定的值(想想 words
的内容,如果扫描失败)。
因此,您将操作分为两部分。
- 使用
fgets()
/ fscanf()
接收输入(如果需要,换行修剪)。检查调用是否成功。
- 将输入缓冲区与所需字符串 (
strcmp()
) 进行比较。
就是说,我真的看不出整个循环有什么意义,因为每次进入循环时都会创建一个新的局部变量 aPlayer
。我希望你知道你在做什么。
忽略上述情况,通用流程应如下所示
input = "Not Done";
while ('input' is not "Done")
scan values;
check for succss;
store into variables;
scan next 'input'
问题出在您的 strcmp() 函数中。事实上,当你这样做时:
strcmp(fscanf(fr, "%s", words),"DONE")
您将 fscanf(它是一个 int)的 return 与 const char *
"DONE" 进行比较。这是不可能的。你需要直接比较 words
和 "DONE"
.
你应该这样做:
int test;
test = fscanf(fr, "%s", words);
while ((test != EOF) && (strcmp(words,"DONE") != 0)) {
float pts;
fscanf(fr, "%f", pts);
float asts;
fscanf(fr, "%f", asts);
float mins;
fscanf(fr, "%f", mins);
struct player *aPlayer;
float theDOC = (pts + asts) / mins;
strcpy(aPlayer->name, words);
aPlayer->DOC = theDOC;
test = fscanf(fr, "%s", words);
}
我在处理特定代码行时遇到了问题,该代码行给出了
的错误error: invalid conversion from
‘int’
to‘const char*’
error: initializing argument 1 of
‘int strcmp(const char*, const char*)’
有谁知道为什么?这是有问题的代码行。
while (strcmp(fscanf(fr, "%s", words), "DONE") != 0)
本质上,我的代码扫描文件(执行某些操作)直到它到达关键字“DONE”(不带引号),然后退出文件。我是初学者 C 程序员,所以请原谅代码中的任何 inaccuracies/inefficiencies。
完整代码如下。
#include <stdio.h>
#include <string.h>
FILE *fr;
struct player {
char name[50];
float DOC;
};
struct player players[50];
int main() {
fr = fopen ("playerinfo.txt", "r");
if (ftell(fr) == 0) {
fclose(fr);
printf("PLAYER FILE IS EMPTY");
return 0;
}
char words[50];
while (strcmp(fscanf(fr, "%s", words),"DONE") != 0) {
float pts;
fscanf(fr, "%f", pts);
float asts;
fscanf(fr, "%f", asts);
float mins;
fscanf(fr, "%f", mins);
struct player *aPlayer;
float theDOC = (pts + asts) / mins;
strcpy(aPlayer->name, words);
aPlayer->DOC = theDOC;
}
fclose(fr);
return 0;
}
在您的代码中,
strcmp(fscanf(fr, "%s", words),"DONE")
并不像你想象的那样。 fscanf()
does not return a pointer to the scanned string, rather, it returns a count (int
type). Your compiler warned you. Read the man page before you proceed.
这种不当使用会导致警告。
也就是说,您必须检查 scanf()
系列函数是否成功,否则,您很有可能最终使用不确定的值(想想 words
的内容,如果扫描失败)。
因此,您将操作分为两部分。
- 使用
fgets()
/fscanf()
接收输入(如果需要,换行修剪)。检查调用是否成功。 - 将输入缓冲区与所需字符串 (
strcmp()
) 进行比较。
就是说,我真的看不出整个循环有什么意义,因为每次进入循环时都会创建一个新的局部变量 aPlayer
。我希望你知道你在做什么。
忽略上述情况,通用流程应如下所示
input = "Not Done";
while ('input' is not "Done")
scan values;
check for succss;
store into variables;
scan next 'input'
问题出在您的 strcmp() 函数中。事实上,当你这样做时:
strcmp(fscanf(fr, "%s", words),"DONE")
您将 fscanf(它是一个 int)的 return 与 const char *
"DONE" 进行比较。这是不可能的。你需要直接比较 words
和 "DONE"
.
你应该这样做:
int test;
test = fscanf(fr, "%s", words);
while ((test != EOF) && (strcmp(words,"DONE") != 0)) {
float pts;
fscanf(fr, "%f", pts);
float asts;
fscanf(fr, "%f", asts);
float mins;
fscanf(fr, "%f", mins);
struct player *aPlayer;
float theDOC = (pts + asts) / mins;
strcpy(aPlayer->name, words);
aPlayer->DOC = theDOC;
test = fscanf(fr, "%s", words);
}