C 读取一个文件并在读取到数组的每一行的开头添加10个数组元素我正在存储文件的行

C 读取一个文件并在读取到数组的每一行的开头添加10个数组元素我正在存储文件的行,c,arrays,file-io,C,Arrays,File Io,我有一个正在读取的文件,并将文件中的字符分别存储到一个数组中。 我的问题是:在读取文件时,如何在读取新行之前向数组中插入10个元素 例如: 文件: 471 22 01 05 34 75 78 65 46 34 20 19 521 01 02 03 45 35 42 36 87 99 12 23 12 37 64 当我读取文件时,它应该是这样结束的: [V0,V1,V2,V3,V4,V5,V6,V7,V8,V9,22,01,05,34,75,78,65,46,34,20,19,V0,V1,V2,V

我有一个正在读取的文件,并将文件中的字符分别存储到一个数组中。 我的问题是:在读取文件时,如何在读取新行之前向数组中插入10个元素

例如:

文件:

471 22 01 05 34 75 78 65 46 34 20 19

521 01 02 03 45 35 42 36 87 99 12 23 12 37 64

当我读取文件时,它应该是这样结束的:

[V0,V1,V2,V3,V4,V5,V6,V7,V8,V9,22,01,05,34,75,78,65,46,34,20,19,V0,V1,V2,V3,V4,V5,V6,V7,V8,V9,01,02,03,45,35,42,36,87,99,12,12,37,64]

我目前的想法是:

在开始读取文件之前,我已经在数组中插入了前10个元素。然后,在读取文件时,每当我找到一个“\0”,我就会使用循环再次插入变量,这就是我得到的:

        Elements = [V0,V1,V2,V3,V4,V5,V6,V7,V8,V9];//array of elements to insert

        int i = 0;
        int j = 0;

        //this loop reads the file and ignores what's not necessary(it's working properly)

        while(s2[i] != '\0')//reading chars from the file
        {
            if(s2[i] == ' ')
                i++;

            MEM[j]=atoi((s2+i));//MEM is the array in which i'm storing the lines

            i += 3;//increment by 3 due to problem specifics(needs to ignore the first 3 values from the file)
            j++;
        }
我知道我的想法不是很有效,但我是C新手,我不知道如何正确地做到这一点

有人知道更好的方法吗?
感谢您显示输入文件包含的内容

471 22 01 05 34 75 78 65 46 34 20 19
521 01 02 03 45 35 42 36 87 99 12 23 12 37 64
您可以使用fscanf逐字读取,而不是逐字符读取,因为显示的文件由类似的类型组成所有的都是数字。从fscanf的手册页

每次通过调用realloc读取一个整数并分配动态内存,在这种情况下,您无需担心数组的大小。下面是从文件中读取数据并存储到整数数组的示例代码

int main(void) {
        FILE *fp = fopen("input.txt","r");
        if(fp == NULL){
                /*.. error handling */
                return 0;
        }
        int row = 1;
        int *input = malloc(sizeof(*input));/*1st time 4 byte, to store first integer inside file */
        /*  here input is dynamic array */
        while(fscanf(fp,"%d",&input[row-1]) == 1) { /* fscanf() read upto whitespace at a time */
                row++;
                input = realloc(input,row * sizeof(*input));/* reallocate based on number of input */
        }
        for(int index = 0 ; index < row-1 ;index++) {
                printf("%d\n",input[index]);
        }
        /* free dynamically allocated memory @TODO*/
        return 0;
}

您的意思是要将文本文件的内容复制到缓冲区或字符数组中,并在每行的开头插入特定数量的字符吗?@wxShayan否,不在文件中。特别是在数组中,我将使用数组,因为我在后面给出了一个例子,你实际上帮助了我阅读部分,但我仍然有最难的部分,数组中的变量插入,你有什么想法吗?我也解释过。fscanf从文件中读取一个整数并存储到输入[0],输入[0]只是数组的一个元素。这里int*input=malloc是动态1D数组。这是您想要的还是其他什么?其他什么对不起,我要添加到数组中的元素不在文件中。阵列需要为这10个元素保留空间。检查我的示例,元素已添加到数组中,但不在文件中
int main(void) {
        FILE *fp = fopen("input.txt","r");
        if(fp == NULL){
                /*.. error handling */
                return 0;
        }
        int row = 1;
        int *input = malloc(sizeof(*input));/*1st time 4 byte, to store first integer inside file */
        /*  here input is dynamic array */
        while(fscanf(fp,"%d",&input[row-1]) == 1) { /* fscanf() read upto whitespace at a time */
                row++;
                input = realloc(input,row * sizeof(*input));/* reallocate based on number of input */
        }
        for(int index = 0 ; index < row-1 ;index++) {
                printf("%d\n",input[index]);
        }
        /* free dynamically allocated memory @TODO*/
        return 0;
}