在C中存储数组中的字符串

时间:2022-01-28 19:59:18

I have read a lot of questions on this, and using them I have altered my code and have created code which I thought would work.

我已经阅读了很多关于这个的问题,使用它们我已经改变了我的代码并创建了我认为可行的代码。

I think it's my understanding of C, which is failing me here as I can't see where I'm going wrong.

我认为这是我对C的理解,这使我失败,因为我无法看到我出错的地方。

I get no compilation errors, but when I run i receive 'FileReader.exe has stopped working' from the command prompt.

我没有编译错误,但是当我运行时,我从命令提示符接收'FileReader.exe已停止工作'。

My code is :

我的代码是:

void storeFile(){
int i = 0;
char allWords [45440][25];
    FILE *fp = fopen("fileToOpen.txt", "r");
    while (i <= 45440){
        char buffer[25];
        fgets(buffer, 25, fp);
        printf("The word read into buffer is : %s",buffer);
        strcpy(allWords[i], buffer);
        printf("The word in allWords[%d] is : %s", i, allWords[i]);
        //allWords[i][strlen(allWords[i])-1] = '\0';
        i = i + 1;
    }
    fclose(fp);

}

There are 45440 lines in the file, and no words longer than 25 char's in length. I'm trying to read each word into a char array named buffer, then store that buffer in an array of char arrays named allWords.

文件中有45440行,长度不超过25个字符。我正在尝试将每个单词读入名为buffer的char数组中,然后将该缓冲区存储在名为allWords的char数组数组中。

I am trying to get this part working, before I refactor to return the array to the main method (which I feel won't be a fun experience).

我试图让这部分工作,然后我重构将数组返回到main方法(我认为这不是一个有趣的体验)。

1 个解决方案

#1


5  

You are trying to allocate more than a megabyte (45440*25) worth of data in automatic storage. On many architectures this results in stack overflow before your file-reading code even gets to run.

您正尝试在自动存储中分配超过兆字节(45440 * 25)的数据。在许多体系结构中,这会导致堆栈溢出,甚至在文件读取代码运行之前。

You can work around this problem by allocating allWords statically, like this

您可以通过静态分配allWords来解决此问题,如下所示

static char allWords [45440][25];

or dynamically, like this:

或者动态地,像这样:

char (*allWords)[25] = malloc(45440 * sizeof(*allWords));

Note that using buffer in the call to fgets is not required, because allWords[i] can be used instead, without strcpy:

请注意,在调用fgets时不需要使用缓冲区,因为可以使用allWords [i]而不使用strcpy:

fgets(allWords[i], sizeof(*allWords)-1, fp);

Also note that an assumption about file size is unnecessary: you can continue calling fgets until it returns NULL; this indicates that the end of the file has been reached, so you can exit the loop using break.

另请注意,关于文件大小的假设是不必要的:您可以继续调用fgets直到它返回NULL;这表示已到达文件的末尾,因此您可以使用break退出循环。

#1


5  

You are trying to allocate more than a megabyte (45440*25) worth of data in automatic storage. On many architectures this results in stack overflow before your file-reading code even gets to run.

您正尝试在自动存储中分配超过兆字节(45440 * 25)的数据。在许多体系结构中,这会导致堆栈溢出,甚至在文件读取代码运行之前。

You can work around this problem by allocating allWords statically, like this

您可以通过静态分配allWords来解决此问题,如下所示

static char allWords [45440][25];

or dynamically, like this:

或者动态地,像这样:

char (*allWords)[25] = malloc(45440 * sizeof(*allWords));

Note that using buffer in the call to fgets is not required, because allWords[i] can be used instead, without strcpy:

请注意,在调用fgets时不需要使用缓冲区,因为可以使用allWords [i]而不使用strcpy:

fgets(allWords[i], sizeof(*allWords)-1, fp);

Also note that an assumption about file size is unnecessary: you can continue calling fgets until it returns NULL; this indicates that the end of the file has been reached, so you can exit the loop using break.

另请注意,关于文件大小的假设是不必要的:您可以继续调用fgets直到它返回NULL;这表示已到达文件的末尾,因此您可以使用break退出循环。