这就是我到目前为止所拥有的.问题是,当"count"变量命中换行符或空格字符时,它没有正确地添加到自身.
#include <stdio.h>
#include <stdlib.h>
int main(void){
char c;
int count = 0;
int* ptr;
int size;
FILE *file = fopen("file.txt", "r"); //Reads from input file
//Test to see if the file exists
if (file == NULL){
printf("ERROR 1: TARGET INPUT FILE DOES NOT EXIST, HAS BEEN RENAMED OR HAS BEEN MOVED.");
exit(1);
}
c = fgetc(file);
while ((c = fgetc(file)) != EOF){
if(c == " " || c == "\n"){
count++;
}
}
fclose(file);
printf("Number of words present in given file: %d", count);
return 0;
}
我不太清楚why是不是失败了,尽管我已经查找了多个来源,其中有几乎完全相同的代码似乎对他们有效.输出应返回一个数字,表示.txt文件中的字数.