到目前为止,这是我的代码
#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 文件中包含一个 wordsearch,因此它只有随机字母。我希望能够做到,这样我就可以说拼图[5][5],它给了我第 4 行和第 4 列中的字符。我遇到了分段错误,但我不知道如何修复它。