I have a file with random characters in basically a word search format. i would like to be able to take all the characters in it and put it into a 2d puzzle array so that i am able to type something like printf("the value is %c",puzzle[2][2]); and it will print the value in the 3rd row and 3rd column (since it starts at 0...) heres my code so far.
#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 = 0;
int rows = 0;
printf("Please enter the table file name: ");
scanf("%s",TableFileName);
/* ... */
TableFilePtr = fopen(TableFileName, "r");
while (fgets(line, sizeof line, TableFilePtr) != NULL && rows < MAXROWS) {
for (cols; cols<(strlen(line)-1) && cols < MAXCOLS; ++cols) {
puzzle[rows][cols] = line[cols];
}
++rows;
}
printf("%c",puzzle[2][2]);
}
the puzzle[x][y] doesnt contain any values once the program runs. any ideas?
update1: changed for cols to for cols=0 update2: added printf("\nthe rows are at %d, cols at %d, puzzle[%d,%d]=%c line[cols]=%c",rows,cols,rows,cols,puzzle[rows,cols],line[cols]); to for loop update3: after update 2, i see the line[cols] characters are getting every character in the puzzle, but not int the correct order. also, the line[cols] isnt correctly being put into the puzzle[rows][cols]. heres some of what what im seeing (not sure why its making me put it as code but whatever):
the rows are at 0, cols at 0, puzzle[0,0]=l line[cols]=A
the rows are at 0, cols at 1, puzzle[0,1]=▒ line[cols]=
the rows are at 0, cols at 2, puzzle[0,2]=▒ line[cols]=B
the rows are at 0, cols at 3, puzzle[0,3]= line[cols]=
the rows are at 0, cols at 4, puzzle[0,4]=\ line[cols]=D
the rows are at 0, cols at 5, puzzle[0,5]=▒ line[cols]=
the rows are at 0, cols at 6, puzzle[0,6]=▒ line[cols]=E
the rows are at 0, cols at 7, puzzle[0,7]= line[cols]=
the rows are at 0, cols at 8, puzzle[0,8]=L line[cols]=Q
the rows are at 0, cols at 9, puzzle[0,9]=▒ line[cols]=
the rows are at 0, cols at 10, puzzle[0,10]=▒ line[cols]=T
the A,B,D,E,A,T are correct but... A should be puzzle[0,0], B should be [0,1] D should be [0,2] etc...
heres a sampler of the input... (would be in a txt file)
K N R D J P L H X E K B W M X A M U Y A L E F Q N D L D
B D B S W W M T F B K Z D G A L V L K U N U R C R I E I
H N L A N D N T O V P O G R U U Y X E D L Y R M Q D T T
Y C G Y E M A S I E X P V Z N W H X B D G G R T K V C W
M Y C X A M I E U T Z J U O C N F F L R E F B T D R Y W
R K V A C B H G L C F D Y X R Z Q E R E H N Q D S J H T
R G E N Y Y K F J V G S C G D H O G K A F E M I S S Q P
S J Z A B V A A P E E P R K F T A H W C G B J N N L W B
F V F Z Y T V Y E O C Y A D L Q Q P P F V W K M E U V O
printfcalls in that loop to print the values of the involved variables. That should help you solve it pretty quick.for (cols; ...)should you docols=0for every iteration?