到目前为止,这是我的代码
#define MAXROWS 60
#define MAXCOLS 60
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
main()
{
char TableFileName[100];
char PuzzleFileName[100];
char puzzle[MAXROWS][MAXCOLS];
char line[MAXCOLS];
FILE *TableFilePtr;
int cols;
int rows;
cols=0;
rows=0;
printf("Please enter the table file name: ");
scanf("%s",TableFileName);
/* ... */
TableFilePtr = fopen(TableFileName, "r");
//printf("\n how many rows and colums are there? separate by a space: ");
// scanf("%d %d",&rows, &cols);
while(fgets(line, sizeof line, TableFilePtr) != NULL)
{
for(cols=0; cols<(strlen(line)-1); ++cols)
{
puzzle[rows][cols] = line[cols];
}
/* I'd give myself enough room in the 2d array for a NULL char in
the last col of every row. You can check for it later to make sure
you're not going out of bounds. You could also
printf("%s\n", puzzle[row]); to print an entire row */
puzzle[rows][cols] = '\0';
++rows;
}
/*int c;
for(c=0; c<MAXROWS; ++c){
fgets(puzzle[rows], sizeof puzzle[rows], TableFilePtr);
}*/
printf("%s",puzzle[5][5]);
}
我想做的是使它从一个文本文件中读取,该文本文件在txt文件中包含一个单词搜索,因此它只有随机字母。我希望能够做到这一点,这样我就可以说Puzzle [5] [5],它使我在第4行和第4列中具有该字符。我遇到了分段错误,但是我不知道如何解决。
最佳答案
您尝试使用printf("%s", puzzle[rows][cols])
打印字符串,并给出char
puzzle[rows][cols]
为1个字符而不是字符串。
执行此操作:printf("%c", puzzle[rows][cols]);
代替。