【问题标题】:Reading lines from c file and putting the strings into an array从 c 文件中读取行并将字符串放入数组中
【发布时间】:2014-10-22 07:22:09
【问题描述】:

我正在尝试将 c 文件的每一行添加到数组中。 files.txt 的内容是

first.c
second.c
third.c
fourth.c

我希望我的代码打印这些行中的每一行,将行添加到我的数组中,然后打印出我的数组中的每个条目。现在它正在正确地执行第一部分,但它只是将第四个.c 添加到数组中。谁能告诉我我的代码有什么问题?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int i=0;
    int numProgs=0;
    char* programs[50];
    char line[50];

    FILE *file;
    file = fopen("files.txt", "r");

    while(fgets(line, sizeof line, file)!=NULL) {
        //check to be sure reading correctly
        printf("%s", line);
        //add each filename into array of programs
        programs[i]=line; 
        i++;
        //count number of programs in file
        numProgs++;
    }

    //check to be sure going into array correctly 
    for (int j=0 ; j<numProgs+1; j++) {
        printf("\n%s", programs[j]);
    }

    fclose(file);
    return 0;
}

【问题讨论】:

  • 你的意思是sizeof(line) 吗?
  • @gargankit sizeof line 也是正确的。
  • 这一行:programs[i]=line;不会工作有两个原因。 1)指向 char 的 50 个指针的数组需要为这 50 个指针中的每一个分配所需的内存(以及指向该内存的指针的设置)。建议您使用 calloc() 以便将内存段预先设置为所有 '\0'。 2) 这行所做的就是将programs[i] 指针设置为指向数组line[]。真正需要的是:strcpy(programs[i], line);

标签: c arrays file-io


【解决方案1】:

你需要改变

programs[i]=line; 

programs[i]=strdup(line); 

否则programs 数组中的所有指针都将指向同一位置(即line)。

顺便说一句:如果 files.txt 包含超过 50 行,你会遇到麻烦。

【讨论】:

    【解决方案2】:

    您需要为每一行分配新的存储空间,否则您只有 1 个行缓冲区来存储文件名,所以只有最后一个显示,在 while 循环中执行此操作:

    programs[i] = calloc(strlen(line)+1, 1);
    strcpy(programs[i], line);
    

    【讨论】:

      【解决方案3】:

      当声明char *programs[50] 时不是有效的指针。因此,您已根据每个行大小为每个指针分配内存。所以,试试这个..(这里我使用的是 malloc 和 strcpy)

          #include <stdio.h>
          #include <stdlib.h>
          #include<string.h>
      
          int main(void) {
          int i=0;
          int numProgs=0;
          char* programs[50];
          char line[50];
      
          FILE *file;
          file = fopen("files.txt", "r");
      
         while(fgets(line, sizeof line, file)!=NULL) {
         //check to be sure reading correctly
          printf("%s", line);
          //add each filename into array of programs
          programs[i]=malloc(sizeof(line));
          strcpy(programs[i],line);
          i++;
         //count number of programs in file
          numProgs++;
        }
      
        //check to be sure going into array correctly 
        for (int j=0 ; j<numProgs+1; j++) {
        printf("\n%s", programs[j]);
       }
      
       fclose(file);
       return 0;
      }
      

      【讨论】:

        【解决方案4】:
        1. 那里不需要i,要保存指向数组行的所有指针,您必须使用strdup()。就这样做吧:

          programs[numProgs++] = strdup(line);

        2. 在第二个循环中条件必须是j &lt; numProgs

        3. 在你的 while 循环中添加额外的条件来防止写入通过数组的结尾:

          while(fgets(line, sizeof line, file)!=NULL &amp;&amp; numProgs &lt; 50)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-11
          • 1970-01-01
          • 2013-12-18
          • 1970-01-01
          • 2013-03-07
          • 2020-08-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多