【发布时间】:2019-05-20 14:45:03
【问题描述】:
我有一个家庭作业,需要我处理 .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。
任何帮助将不胜感激。
【问题讨论】:
-
如果文件中只有 12 行,尝试打印第 96 行会发生什么?
-
为什么不直接获取文件的大小,然后在读取文件之前为其分配所需的内存?看这里:stackoverflow.com/questions/238603/… 一旦你有了文件的大小,然后调用 malloc 并读入缓冲区。而且您的代码没有释放任何分配的内存。
-
这段代码到处都有神奇的常量
-
@Nina 这可能是一种锻炼,他必须这样做。