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

标签 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。

如有任何帮助,我们将不胜感激。

最佳答案

这里有一个问题:

//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;

顺便说一句3:

while (fgets(buffer, 3000, inputFile))

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

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

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

BTW4:最后你只打印你读过的前 100 行。您应该能够自己纠正这个问题。

顺便说一句:您还应该释放已分配的所有内存。我把这个作为练习留给你。提示:大约需要在 main 末尾添加三行代码。

关于无法使用 fgets 将 .txt 文件扫描为灵活的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56223322/

相关文章:

c - 如何从C中的数组中获取值?

c - C 中的隐式函数声明

c - 总线错误 : 10 in C while inputing text from . txt 文件进入节点

c - 从文件读取并使用 strtok while 循环仅打印每行的第一个单词

c - 我在 eclipse 中使用 stdin 和 NULL 时遇到问题

c - 需要查找一个序列的成本

子级从 std 输入读取并写入 std 输出

c++ - 在 Windows 上找不到 valloc 标识符

c - 将单词读入链接列表

linux popen 卡在 fgets 上