我有一个只有行和列中的随机字母的文本文件。我想要做的就是制作一个二维数组,这样它的puzzle[i][j]
如果我把printf("%c", puzzle[5][4]);
它简单地给我第四行和第三列字符(因为它从一个数组中的0开始)。这是我的代码到目前为止。将文本文件读入二维数组
#define MAXROWS 60
#define MAXCOLS 60
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
main()
{
FILE *TableFilePtr;
char TableFileName[100];
char PuzzleFileName[100];
char puzzle[MAXROWS][MAXCOLS];
printf("Please enter the table file name: ");
scanf("%s",TableFileName);
TableFilePtr=fopen(TableFileName, "r");
if(TableFilePtr == NULL)
{
printf("Can't open %s", TableFileName);
exit(EXIT_FAILURE);
}
char words;
int n;
n=0;
int i,j,row,col;
int rowcount, colcount;
printf("\n how many rows and colums are there? separate by a space: ");
scanf("%d %d",&row, &col);
/* while(fscanf(TableFilePtr,"%c",&words)!= EOF)
{
printf("%c",words);
}
*/
/*for (colcount=0;colcount<col;colcount++)
{
for (rowcount=0;rowcount<row;rowcount++)
{
printf("%c ",words);
}
printf("\n");
}
*/
for(i=0;i<row;i++){
for(j=0;j<col;j++){
fscanf(TableFilePtr, "%c %s\n",&puzzle[i]][j]);
//puzzle[i][j]=words;
// printf("%c ", puzzle[i][j]);
}
printf("\n");
}
}
末注释的区域(刚开始的部分)的作品简单地打印出的文本文件中的编译器。尽管如此,我想让它成为2d数组。
for(colcount=0;colcount<col;colcount++){...}
uve混淆了你对数组索引的理解,是一个错字? – 2013-03-22 01:22:42
请注意,'拼图[5] [4]'在第六行和第五列打印值,而不是第四行和第三列的值 - 正是因为索引从0开始。 – 2015-12-17 16:53:57