は、私が試した:Cを使用してファイルから2次元配列を読み取るにはどうすればよいですか?
void read_grid_from_file(int** grid, const size_t row, const size_t column, FILE* inf) {
size_t x, y;
for(x = 0; x < row; ++x) {
for(y = 0; y < column; ++y) {
fscanf(inf, "%d", &grid[x][y]);
printf("%d ", grid[x][y]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
FILE* inf; // input file stream
FILE* outf; // output file stream
char pbm_name[20];
size_t row = 0;
size_t column = 0;
/*
if(argc != 3) {
prn_info(argv[0]);
exit(1);
}
*/
inf = fopen("infile.txt" , "r");
outf = fopen("outfile.txt", "w");
fgets(pbm_name, 20, inf);
fscanf(inf, "%d", &row);
fscanf(inf, "%d", &column);
int** grid = allocate_memory_for_grid(row, column);
read_grid_from_file(grid, row, column, inf);
show_grid(grid, row, column); //for debugging
}
入力ファイルがある:
P1
12 14
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
出力は次のようになります。その行列は
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1
Press any key to continue . . .
をどこから来ましたの?
ありがとうございましたJay! – Chan
あなたはようこそです。 +1はできませんか? ; ) – Jay