如何将.txt文件逐行读取到C数组中? [英] How to read a .txt file into C array line by line?

查看:41
本文介绍了如何将.txt文件逐行读取到C数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Members.txt 的.txt文件,其中包含:

i have a .txt file named Members.txt which contain:

2
Rebaz salimi 3840221821 0918888888
Hojjat Qolami 2459816431 09177777777

我写了一个C文件,将 Members.txt 读入 char w [100]; 数组中,例如:

i had written a C file to read Members.txt into 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;
}

但是,我需要将 Members.txt 的每一行换行逐行保存到不同的数组中.

but ,i need every newline ofMembers.txt to be saved into different array line by line.

推荐答案

如果要读取文件并将其存储在数组中,则可以采用以下解决方案:无法存储在数组中,但是可以存储在数组的内部.在这里,我使您可以访问100行文本文件.无论如何,这是代码:

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. 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);
        }
    }
}

这是结果:

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..

这篇关于如何将.txt文件逐行读取到C数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆