0

我正在尝试使用 C 用 fscanf 读取一个大文本文件(例如,一本超过一万字的小说),但是每次程序都会读取到某个点并且似乎达到 EOF,而它距离很远文件结尾。这是我的代码:

void putWordsInArray(){

        FILE* fileToRead;
        char words[1024];
        int singleSize = 0;

        singleWordArray = (char**)malloc(1024*sizeof(char*));
        indexArray1 = (int*)malloc(1024*sizeof(int));
        //read the temp file automatically generated
        fileToRead = fopen("temp.txt", "r");
        while(fscanf(fileToRead, "%s", words) != EOF){

            printf("%s\n", words);
            //the following if-else statement add the word read to the single word array
            //if they are not yet in the array, or increment its count by 1 isf its already there
            int result = -1;
            result = contains(words, singleWordArray, singleSize);
            if(result == -1){
                indexArray1 = (int*)realloc(indexArray1, (singleSize + 1) * sizeof(int));
                singleWordArray = (char**)realloc(singleWordArray, (singleSize + 1) * sizeof(char*));
                singleWordArray[singleSize] = (char*)calloc((strlen(words) + 1), 1024*sizeof(char));
                strcpy(singleWordArray[singleSize], words);
                indexArray1[singleSize] = 1;
                singleSize++;

            }
            else{
                indexArray1[result] += 1;
            }
            //check if the current list contains the incoming word or not
        }
    }

有谁知道这里发生了什么?我相信我每次都分配了足够的内存。感谢任何帮助的人。

4

0 回答 0