Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从c文件中读取行并将字符串放入数组_C_Arrays_File Io - Fatal编程技术网

从c文件中读取行并将字符串放入数组

从c文件中读取行并将字符串放入数组,c,arrays,file-io,C,Arrays,File Io,我试图将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 num

我试图将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;
}
#包括
#包括
内部主(空){
int i=0;
int numProgs=0;
char*程序[50];
字符行[50];
文件*文件;
file=fopen(“files.txt”、“r”);
while(fgets(行、行大小、文件)!=NULL){
//检查以确保读数正确
printf(“%s”,第行);
//将每个文件名添加到程序数组中
程序[i]=行;
i++;
//计算文件中的程序数
numProgs++;
}
//检查以确保正确进入阵列
对于(int j=0;j您需要更改

programs[i]=line; 

否则
程序
数组中的所有指针将指向同一位置(即


顺便说一句:如果files.txt包含超过50行,您将遇到问题。

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

programs[i] = calloc(strlen(line)+1, 1);
strcpy(programs[i], line);
  • 不需要
    i
    ,要保存数组中所有指向行的指针,必须使用
    strdup()
    。只需这样做:

    程序[numProgs++]=strdup(行);

  • 在第二个循环中,条件必须是
    j

  • 向while循环中添加其他条件,以防止写入数组末尾:

    while(fgets(行、行大小、文件)!=NULL&&numProgs<50)


  • 当声明
    char*程序[50]
    不是有效的指针时。因此,您必须根据每行大小为每个指针分配内存。因此,尝试一下这个..(这里我使用malloc&strcpy)

    #包括
    #包括
    #包括
    内部主(空){
    int i=0;
    int numProgs=0;
    char*程序[50];
    字符行[50];
    文件*文件;
    file=fopen(“files.txt”、“r”);
    while(fgets(行、行大小、文件)!=NULL){
    //检查以确保读数正确
    printf(“%s”,第行);
    //将每个文件名添加到程序数组中
    程序[i]=malloc(sizeof(line));
    strcpy(程序[i],行);
    i++;
    //计算文件中的程序数
    numProgs++;
    }
    //检查以确保正确进入阵列
    
    for(int j=0;jdid u mean
    sizeof(line)
    ?@gargankit
    sizeof line
    也是正确的。此行:programs[i]=line;由于两个原因无法工作。1)指向char的50个指针数组需要有所需的内存分配(以及设置指向该内存的指针)对于这50个指针中的每一个。建议您使用calloc(),这样内存段将被预先设置为所有“\0”。2)此行所做的只是将程序[i]指针设置为指向数组行[]。真正需要的是:strcpy(程序[i],行);
    programs[i] = calloc(strlen(line)+1, 1);
    strcpy(programs[i], line);
    
        #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;
    }