如何在C中的数组中存储文本文件中的字符串

时间:2021-05-18 21:21:29

I want to read a text file into a string array and be able to access the array contents through a loop. The code I have allows me to store only the last line of the text file instead of the entire file; where am I going wrong?

我想将文本文件读入字符串数组,并能够通过循环访问数组内容。我的代码允许我只存储文本文件的最后一行而不是整个文件;我哪里错了?

#define MAX 10000

int main (int argc, char *argv[])
{
    FILE *fp;
    char str[MAX];
    char *x[MAX];
    int i =0;
    char y[MAX];

    if((fp = fopen("550.txt", "r"))==NULL) {
    printf("Cannot open file.\n");
    exit(1);}

    while(!feof(fp)) {
        while(fgets(str, sizeof str, fp)) {
            x[i]= str;
            printf("%s", str);
            printf("%s", *(x+i));
            i++;
        }
  }


for(i=0;i<100;i++){
    printf("%s", *(x+i));
}

   fclose(fp);

  return 0;
}

3 个解决方案

#1


3  

You only allocate one string array str. At each iteration through the loop you are simply overwriting str. The assignment x[i] = str assigns the pointer value of str to x[i]. You'll notice that each member of the array x points to the same buffer str at the end of the loop. You need to create multiple buffers.

您只分配一个字符串数组str。在循环的每次迭代中,您只需覆盖str。赋值x [i] = str将str的指针值赋给x [i]。您会注意到数组x的每个成员在循环结束时指向相同的缓冲区str。您需要创建多个缓冲区。

One way to do this is to define the maximum number of lines using #define LINES 100 and then to declare x as follows

一种方法是使用#define LINES 100定义最大行数,然后按如下方式声明x

char x[LINES][MAX];

and then perform a strcpy at each iteration:

然后在每次迭代时执行strcpy:

while(fgets(str, sizeof str, fp)) {
    strcpy(x[i], str);
    printf("%s", str);
    printf("%s", *(x+i));
    i++;
}

Note that you should consider using the strncpy method instead of strcpy and check the return value to ensure that the buffers do not overrun.

请注意,您应该考虑使用strncpy方法而不是strcpy,并检查返回值以确保缓冲区不会溢出。

#2


1  

You need to make a copy of each string:

您需要复制每个字符串:

x[i] = strdup(str);

What you're doing at the moment is making each x[i] point to the same buffer, which will contain the last line of the file once you've finished.

你现在正在做的是让每个x [i]指向同一个缓冲区,一旦你完成,它将包含文件的最后一行。

(Note: you'll also need to free() all of the x[i] strings you create with strdup().)

(注意:你还需要释放()使用strdup()创建的所有x [i]字符串。)

#3


0  

you are not stroing the line you read anywhere. It is buffered into str but the you overwrite it with the next line. use strdup.

你没有在任何地方读到你读的那条线。它被缓冲到str但是你用下一行覆盖它。使用strdup。

x[i] = strdup(str);

#1


3  

You only allocate one string array str. At each iteration through the loop you are simply overwriting str. The assignment x[i] = str assigns the pointer value of str to x[i]. You'll notice that each member of the array x points to the same buffer str at the end of the loop. You need to create multiple buffers.

您只分配一个字符串数组str。在循环的每次迭代中,您只需覆盖str。赋值x [i] = str将str的指针值赋给x [i]。您会注意到数组x的每个成员在循环结束时指向相同的缓冲区str。您需要创建多个缓冲区。

One way to do this is to define the maximum number of lines using #define LINES 100 and then to declare x as follows

一种方法是使用#define LINES 100定义最大行数,然后按如下方式声明x

char x[LINES][MAX];

and then perform a strcpy at each iteration:

然后在每次迭代时执行strcpy:

while(fgets(str, sizeof str, fp)) {
    strcpy(x[i], str);
    printf("%s", str);
    printf("%s", *(x+i));
    i++;
}

Note that you should consider using the strncpy method instead of strcpy and check the return value to ensure that the buffers do not overrun.

请注意,您应该考虑使用strncpy方法而不是strcpy,并检查返回值以确保缓冲区不会溢出。

#2


1  

You need to make a copy of each string:

您需要复制每个字符串:

x[i] = strdup(str);

What you're doing at the moment is making each x[i] point to the same buffer, which will contain the last line of the file once you've finished.

你现在正在做的是让每个x [i]指向同一个缓冲区,一旦你完成,它将包含文件的最后一行。

(Note: you'll also need to free() all of the x[i] strings you create with strdup().)

(注意:你还需要释放()使用strdup()创建的所有x [i]字符串。)

#3


0  

you are not stroing the line you read anywhere. It is buffered into str but the you overwrite it with the next line. use strdup.

你没有在任何地方读到你读的那条线。它被缓冲到str但是你用下一行覆盖它。使用strdup。

x[i] = strdup(str);