Hey guys, my C programming class has been cancelled for one reason or another for the past three weeks so I'm trying to learn some of this crap on my own before my assignment is due. I'm really struggling with reading a txt file into a 2D array and can't find any snippets of code online that are really helping me.
here is my text file:
This is obviously a word search puzzle.
I would like to read this into two separate 2D arrays (one for the puzzle and it will always be an nxn puzzle and n is not defined as any puzzle and word list can be used (as long as the puzzle is nxn).
here's what I have so far in an attempt to read this into an array:
and it outputs:
Any help would be greatly appreciated!
here is my text file:
Code:
T A E D Q Q Z H P N I U C K E W D I V U X O F C B P I R G K N R T B R B EXIT THE QUICK BROWN FOX
I would like to read this into two separate 2D arrays (one for the puzzle and it will always be an nxn puzzle and n is not defined as any puzzle and word list can be used (as long as the puzzle is nxn).
here's what I have so far in an attempt to read this into an array:
Code:
#include <stdio.h>
#include <string.h>
int main()
{
FILE *wsPtr;
int puzzle[50][50];
int i;
int j;
wsPtr = fopen("test.txt", "r");
for(i = 0; puzzle[i][j] != EOF; i++)
{
for(j = 0; puzzle[i][j] != '\0'; j++)
{
puzzle[i][j] = fgetc(wsPtr);
printf("%c", puzzle[i][j]);
j++;
}
i++;
}
fclose(wsPtr);
return 0;
}
Code:
T A E D Q Q Z H P N I U C K E W D I Segmentation fault (core dumped)













Comment