Reputation: 73
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
Upvotes: 0
Views: 1784
Reputation: 40145
test.c
#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");
while (fgets(line, sizeof line, TableFilePtr) != NULL && rows < MAXROWS) {
char *p=line;
for(cols=0;NULL != (p=strtok(p, " \n")) && cols < MAXCOLS; ++cols){
puzzle[rows][cols]=*p;
p=NULL;
}
++rows;
}
{ //check puzzle[][] print out
int r, c;
for(r=0;r<rows;r++){
for(c=0;c<cols;c++){
printf("%c", puzzle[r][c]);
}
printf("\n");
}
}
printf("%c",puzzle[2][2]);
}
data.txt
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
execute program result:
Please enter the table file name: data.txt
KNRDJPLHXEKBWMXAMUYALEFQNDLD
BDBSWWMTFBKZDGALVLKUNURCRIEI
HNLANDNTOVPOGRUUYXEDLYRMQDTT
YCGYEMASIEXPVZNWHXBDGGRTKVCW
MYCXAMIEUTZJUOCNFFLREFBTDRYW
RKVACBHGLCFDYXRZQEREHNQDSJHT
RGENYYKFJVGSCGDHOGKAFEMISSQP
SJZABVAAPEEPRKFTAHWCGBJNNLWB
FVFZYTVYEOCYADLQQPPFVWKMEUVO
L
Upvotes: 1
Reputation: 40145
while (fgets(line, sizeof line, TableFilePtr) != NULL && rows < MAXROWS) {
/*
for (cols=0; cols<(strlen(line)-1) && cols < MAXCOLS; ++cols) {
puzzle[rows][cols] = line[cols];
}
*/
char *p=line;
for(cols=0;NULL != (p=strtok(p, " \n")) && cols < MAXCOLS; ++cols){
puzzle[rows][cols]=*p;
p=NULL;
}
++rows;
}
test print code sample:
{ //check puzzle[][] print out
int r, c;
for(r=0;r<rows;r++){
for(c=0;c<cols;c++){
printf("%c", puzzle[r][c]);//do you mistake like this puzzle[r,c] ?
}
printf("\n");
}
}
Upvotes: -1
Reputation: 311
when I run your code, I am getting real values from my text file in the puzzle array. I think you're just mistaken about where you're putting those values. I can't be sure what you're doing since you didn't include an example of what you're parsing, but I'm definitely getting values in my puzzle array.
if my file looks like this : 1 2 3 4 5 6 7 8 9 10 then the first three elements of puzzle[0] are "1", " ", "2" and all other elements of puzzle are null.
After playing around for a bit I realized what seems to be the problem. You've declared cols up top, and it's being reused inside the loop. So when rows gets incremented, you're not resetting cols. Add cols = 0; after rows++ and the array seems to behave the way you need it to.
Edit: mostly style things.
For one thing, you're using a for loop inside of a while loop, which seems strange to me for iterating over a multidimensional array. Don't get me wrong- they're basically the same thing. But for readability, it usually makes more sense to me to do while(while) or for(for) when you're doing something like this with rows and columns.
Also, instead of resetting cols and incrementing rows at the end of the while loop, try changing it to a for loop. If you declare the incrementers in the declaration of the for loop, you won't have to worry as much about making these mistakes, and it becomes more readable IMO.
Also, one last note for now - maybe the thing you didn't realize was that you have to be very careful using sizeof on a char array. I wouldn't use sizeof as the maxint for fgets, as sizeof includes the null terminator in a char array (see this post C++: size of a char array using sizeof)
I'm not a c expert so there's probably a lot more to be said here (there are a ton of ways to get lines from a file and I don't think fgets is the reccomended route) but that's about all I can think of for now, and it will hopefully at least help you get it running properly.
Upvotes: 0