【发布时间】:2017-04-08 13:03:16
【问题描述】:
我知道你可以只关闭一个文件,但我想尝试使用倒带功能,但我遇到了一个奇怪的错误。首先我读取一个文件并计算字数,然后我尝试倒带(只是为了练习文件处理),输出以下错误:看起来问题出在最后一行代码。
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int readFile(FILE *f, char *fileName) {
int count = 0;
char ch;
f = fopen(fileName, "r");
if ( f == NULL ) {
printf("Cannot open %s file, please verify it's in the right location\n", fileName);
}
while ( (ch = fgetc(f) ) != EOF ) {
if ( ch == '\n' ) {
count++;
}
}
printf("The count number is %d", count);
return count;
}
int main() {
FILE *wordInput = NULL;
int i, j, k = 0;
char c;
char *point; // pointer that points to a word
char **dictionary; // pointer that points to variable point
int count = 0;
int dictChoice; // which dictionary are they picking
int numLetters = 4; // number of letters for each word
FILE *fPoint = NULL;
char *name = "smallDictionary.txt";
readFile(wordInput, name);
rewind(wordInput);
return 0;
}
【问题讨论】:
-
我去掉了 close 语句,它仍然可以这样做
-
C 是按值传递。
wordInputinmain()被初始化为NULL并且永远不会改变它的值。 -
顺便说一句,
fgetc()返回int而不是char。 -
能够读取 256 个不同的字符,并且能够返回一个单独的值来识别这 8 位之上的文件结尾是不够的。
-
"that's not working" 到底是什么意思?你如何测试?会发生什么?