我知道你可以关闭一个文件,但我想尝试使用倒带功能,但我得到了一个奇怪的错误。首先,我读取一个文件并计算单词数,然后我尝试快退(只是为了练习文件处理),并输出以下错误:看起来问题是最后一行代码。
以下是代码:
#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;
}
答案 0 :(得分:1)
int readFile(FILE *f, char *fileName)
由于您尝试修改FILE
指针,因此需要将指针传递给指向FILE
或FILE **
的指针。将功能标题更改为
int readFile(FILE **f, char *fileName)
在调用者中,您需要将指针传递给FILE *
对象:
FILE *pf;
int n = readFile(&pf, "filename.txt");
此外,当您完成操作文件后,立即致电fclose
fclose(pf);