从文件填充二维数组,其中参数是前两个值 [英] Filling a 2d array from file, where the parameters are the first two values
问题描述
数据文件是
6 3
6 7 8
1 2 3
7 8 9
3 4 5
8 9 0
1 6 8
--------- ---
#include< stdio.h>
#include< stdlib.h>
int
main(无效)
{
int a,b,i = 0,j = 0,x [i] [j];
FILE * f1;
f1 = fopen(" mat1.dat"," r");
fscanf(f1,%d%d,& a,& b);
printf(行:%d,列:%d \ n,a,b);
>
for(int i = 0; i< a; i = i + 1)
{
for(int j = 0; j< b; j = j + 1)
{
fscanf(f1,"%d",& x [i] [j]);
}
printf(" \ n");
}
fclose( f1);
返回(0);
}
它不起作用。我如何读取矩阵并忽略前两个值?代码是否正确?
The data file is
6 3
6 7 8
1 2 3
7 8 9
3 4 5
8 9 0
1 6 8
------------
#include <stdio.h>
#include <stdlib.h>
int
main (void)
{
int a, b,i=0,j=0, x[i][j];
FILE *f1;
f1 = fopen ("mat1.dat", "r");
fscanf (f1, "%d%d", &a, &b);
printf ("Rows: %d, Columns: %d\n", a, b);
for (int i=0; i<a; i=i+1)
{
for (int j=0; j<b; j=j+1)
{
fscanf (f1, "%d", &x[i][j]);
}
printf ("\n");
}
fclose (f1);
return (0);
}
It doesnt work. How do i read the matrix and ignore the first two values? Is the code correct?
推荐答案
你的问题是什么?
我一直得到
1 6 8
1 6 8
1 6 8
1 6 8
1 6 8
1 6 8
I keep getting
1 6 8
1 6 8
1 6 8
1 6 8
1 6 8
1 6 8
看看数组显示。
您使用的是C还是C ++?
如果是C,您知道您是否使用C99吗? br />
你声明x有0行0列,但是你继续用很多行和列来存储值。
Take a look at Arrays Revealed.
Are you using C or C++?
If C, do you know if you are using C99?
You declare x as having 0 rows and 0 columns, but then you go on to store values in lots of rows and columns.
这篇关于从文件填充二维数组,其中参数是前两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!