我知道这可能是一个简单的错误,但我仍然无法弄清楚错误.我正进入(状态
当我打印出整数时,指针地址而不是值.
#include<stdio.h>
int main(){
char s1[100];
int words,lines,chara = 0;
FILE * fp;
fp = fopen("fox.txt","r");
if(fp==NULL){
printf("Can't open file");
}
else{
while (fscanf(fp,"%s",s1) != EOF){
words++;
// printf("%s",s1);
}
}
printf("There are %d of words",words);
}
Run Code Online (Sandbox Code Playgroud)
fox.txt
The quick
brown fox
jumps over
the lazy
dog
Run Code Online (Sandbox Code Playgroud)
输出:
There are 2665625 of words
Run Code Online (Sandbox Code Playgroud)
sim*_*onc 11
words 未初始化,因此您从未定义的值开始递增.
int words,lines,chara = 0;
Run Code Online (Sandbox Code Playgroud)
设置chara为0但不初始化其他变量.如果你想初始化所有3,你需要
int words = 0, lines = 0, chara = 0;
Run Code Online (Sandbox Code Playgroud)