无法使用fgets扫描.txt文件到灵活的数据结构中

时间:2019-05-20 14:45:04

标签: c malloc fgets

我有一项家庭作业,要求我处理.txt文件,方法是将它们扫描到灵活的数据结构中,然后在文件中搜索大写字母的单词。我在使用的这种灵活数据结构中扫描它们时遇到问题。数据结构需要灵活的原因是它需要能够处理任何.txt文件。

我要使用的数据结构是一个数组,该数组指向包含行内容的数组。如果更容易,我愿意使用其他结构。

我尝试使用fgets逐行对其进行扫描,并使用malloc分配足够的空间来存储该行,但这似乎行不通。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STEPSIZE 100

int main()
{
    FILE *inputFile;

    //Opens the file in read mode
    inputFile = fopen("testfile.txt", "r");

    //Error message if file cannot open
    if (inputFile == NULL)
    {
        printf("Unable to open file");
        return 1;
    }

    int arrayLen = STEPSIZE;

    // Allocate space for 100 lines. The **lines is the data structure used to store all the lines

    char **lines = (char **)malloc(STEPSIZE * sizeof(char*));

    char buffer[3000];

    int i = 0;

    while (fgets(buffer, 3000, inputFile))
    {

        //Checks if the array is full, and extends it
        if(i == arrayLen)
        {
            arrayLen += arrayLen;
            char ** newLines = realloc(lines, 200 * sizeof(char*));
            if(!newLines)
            {
                printf("cant realloc\n");
            }
            lines= newLines;
        }


        // Get length of buffer
        int lengthOfBuffer = strlen(buffer);

        //Allocate space for string. The +1 is for the terminating character
        char *string = (char *)malloc((lengthOfBuffer + 1) * sizeof(char));

        //copy string from buffer to string
        strcpy(string, buffer);

        //Attach string to data structure
        lines[i] = string;

        //Increment counter
        i++;
        printf("%s", lines[i]);
    }

    //Closes the file
    fclose(inputFile);


    for (int j = 0; j < 100; j++){
        printf("%s \n", lines[i]);
    }

    return 0;
}

运行最终的for循环时,理想情况下将打印文件的内容,只是为了表明它已经存储并且可以处理,但是目前我得到退出代码11。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这里有个问题:

//Increment counter
i++;
printf("%s", lines[i]);    // you're printing the next file that does not yet exist

正确的代码:

printf("%s", lines[i]);
//Increment counter
i++;

另外一个在这里:

for (int j = 0; j < 100; j++) {  // your loop variable is j
  printf("%s \n", lines[i]);     // but you use i here.
}

正确的代码:

for (int i = 0; i < 100; i++) {
  printf("%s \n", lines[i]);
}

这里还有另一个:

  arrayLen += arrayLen;
  char ** newLines = (char**)realloc(lines, 200 * sizeof(char*));
  // here the new length of your array is inconditionally 200
  // but actually the new array length is arrayLen 

正确的代码:

  arrayLen += arrayLen;
  char ** newLines = (char**)realloc(lines, arrayLen * sizeof(char*));

可能还有更多问题,我没有检查所有内容。

顺便说一句:sizeof(char)根据定义为1,因此您可以将其删除。

BTW2:arrayLen += arrayLen;您确定这是您想要的吗?每次将数组的大小加倍。这不一定是错误的,但是使用此方法,数组长度将很快增长到很大的数量。您可能想要这样:arrayLen += STEPSIZE;

BTW3:

while (fgets(buffer, 3000, inputFile))

这实际上并没有错,但是您最好这样写:

while (fgets(buffer, sizeof buffer, inputFile))

消除了两个硬编码常量3000之一。

BTW4:最后,您只打印您已阅读的前100行。您应该可以自行纠正。

BTW5:您还应该释放所有已分配的内存。我将此作为练习留给您。提示:在main的末尾添加大约三行代码。