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
fscanf在读取文件时复制数字_C_Arrays_Sorting_Scanf - Fatal编程技术网

fscanf在读取文件时复制数字

fscanf在读取文件时复制数字,c,arrays,sorting,scanf,C,Arrays,Sorting,Scanf,我试图从文件中读取数字并将其放入2d数组中,但每行的第一个数字被放入上一行的最后一个数字的位置 输出应如下所示: 11 14 12 07 1 8.7 11 14 11 58 143 8.6 11 14 13 03 163 8.9 11 13 18 06 1 7.3 11 14 12 07 1 11 11 14 11 58 143 11 11 14 13 03 163 11 11 13 18 06 1 7.3 但结

我试图从文件中读取数字并将其放入2d数组中,但每行的第一个数字被放入上一行的最后一个数字的位置

输出应如下所示:

11  14  12  07  1   8.7
11  14  11  58  143 8.6
11  14  13  03  163 8.9
11  13  18  06  1   7.3
11  14  12  07  1   11
11  14  11  58  143 11
11  14  13  03  163 11
11  13  18  06  1   7.3
但结果却是这样的:

11  14  12  07  1   8.7
11  14  11  58  143 8.6
11  14  13  03  163 8.9
11  13  18  06  1   7.3
11  14  12  07  1   11
11  14  11  58  143 11
11  14  13  03  163 11
11  13  18  06  1   7.3
这是将值放入数组的循环:

    double all_data[entry_counter-1][5];
    int col_counter = 0;
    int row_counter = 0;

    for(int i=0; i<=entry_counter/6-1; i++) {
        col_counter = 0;
        for (int j = 0; j <= 5; j++) {
            fscanf(input_file, "%lf\n", &v);

            all_data[row_counter][col_counter] = v;
            col_counter++;
        }
        row_counter++;
    }

所有数据加倍[输入计数器-1][5];
int col_计数器=0;
int row_计数器=0;

对于(inti=0;i,您的代码过于复杂和错误

这应该可以做到:

int cols = 6;
int rows = entry_counter / cols;
double all_data[rows][cols];

for(int row_counter = 0; row_counter < rows; row_counter++) {
    for (int col_counter = 0; col_counter < cols; col_counter++) {
        fscanf(input_file, "%lf\n", &all_data[row_counter][col_counter]);
    }
}
int cols=6;
int rows=输入\计数器/列;
将所有_数据[行][列]加倍;
对于(int row_counter=0;row_counter

保持简单。

没有a,看起来像是边界溢出场景。数组每行只有5个项目的空间,索引从0到4。但是
j
循环计数从0到5。顺便说一句,不需要单独的
col_计数器
,因为
j
col_计数器
总是具有相同的值。你是想使用所有数据加倍[输入计数器-1][6]