简体   繁体   English

如何将.txt文件逐行读取到C数组中?

[英]How to read a .txt file into C array line by line?

i have a .txt file named Members.txt which contain: 我有一个名为Members.txt的.txt文件,其中包含:

2
Rebaz salimi 3840221821 0918888888
Hojjat Qolami 2459816431 09177777777

i had written a C file to read Members.txt into char w[100]; 我已经编写了一个C文件,将Members.txt读入char w[100]; array like: 数组像:

int main()
{
       int i = 0, line = 5;
       char w[100];
       char f[20];
       char k[15];
       FILE *myfile;
                      myfile = fopen("Members.txt","r");
                      if (myfile== NULL)
                      {
                       printf("can not open file \n");
                       return 1;
                      }

     while(line--){
                   fscanf(myfile,"%s",&w[i]);
                   i++;
                   printf("\n%s", &w[i]);
                  }
                   fclose(myfile);
        return 0;
}

but ,i need every newline of Members.txt to be saved into different array line by line. 但是,我需要将Members.txt每个换行符Members.txt保存到不同的数组中。

Here is the solution if you want to read the file and store inside array, You cannot store inside array, but you can store inside structure of array. 如果您想读取文件并存储在数组内部,这是解决方案,您不能在数组内部存储,但是可以在数组内部存储。 Here I make you can access 100 lines of text file. 在这里,我使您可以访问100行文本文件。 Here is the code anyway: 无论如何,这是代码:

#include <stdio.h>

//Use Structure to store more than one data type
//Since your file not only consist of string, it also have int
struct members
{
    char a[100];
    char b[100];
    long long int c;
    long long int d;
};
//Here I make 100 line so that you can read 100 line of text file
struct members cur_member[100];

int main(void) {
    FILE *myfile = fopen("Members.txt", "r");
    if (myfile == NULL) {
        printf("Cannot open file.\n");
        return 1;
    }
    else {
        //Check for number of line
            char ch;
            int count = 0;
        do
        {
        ch = fgetc(myfile);
        if (ch == '\n') count++;
        } while (ch != EOF);
        rewind(myfile);

        //Since you put 2 earlier in the member.txt we need to dump it
        //so that it wont affect the scanning process
        int temp;
        fscanf(myfile, "%d", &temp);
        printf("%d\n", temp);
        //Now scan all the line inside the text
        int i;
        for (i = 0; i < count; i++) {
            fscanf(myfile, "%s %s %lld %lld\n", cur_member[i].a, cur_member[i].b, &cur_member[i].c, &cur_member[i].d);
            printf("%s %s %lld %lld\n", cur_member[i].a, cur_member[i].b, cur_member[i].c, cur_member[i].d);
        }
    }
}

And this is the result: 结果如下:

2
Rebaz salimi 3840221821 918888888
Hojjat Qolami 2459816431 9177777777
Press any key to continue . . .

This program will read your current file and I just print it, to show it works. 该程序将读取您的当前文件,而我只是将其打印出来,以显示其工作原理。 You can access the information and edit the file. 您可以访问信息并编辑文件。 Thats all.. 就这样..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM