0

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 
11
  • 1
    Do you use any debugger? Commented Mar 25, 2013 at 3:39
  • Put some printf calls in that loop to print the values of the involved variables. That should help you solve it pretty quick. Commented Mar 25, 2013 at 3:40
  • Try debugging your program to see where it went wrong :) gdb or Visual Studio. Commented Mar 25, 2013 at 3:49
  • it goes wrong with the results it gets. i "self debugged" it and the results for the puzzle[rows][cols] are either blank spaces, random numbers, random letters, or the ▒ symbol Commented Mar 25, 2013 at 3:51
  • 3
    for (cols; ...) should you do cols=0 for every iteration? Commented Mar 25, 2013 at 3:55

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

9 Comments

is it possible to make it so that it works for all inputs of all rows & columns? i need it to work with 20 rows & 20 columns, 40 rows & 40 columns, and 60 rows & 60 columns. now that im looking at the output of my code, i see that its taking the first 28 or so characters and putting it in the puzzle, then its taking the next 28 and putting it into the next row, even though it is part of the first row. which part of the code declared when to make a new row?
im also using this code in line 36 to make everything easier to see printf("\nrows=%d cols=%d puzzle[%d][%d] = %c", r,c,r,c,puzzle[r][c]);
possible until 60*60, because char puzzle[MAXROWS][MAXCOLS]; MAXROWS is 60 and MAXCOLS is 60
then why does it stop at puzzle[0][28] and not just keep going?
@Alex first line data is "KNRDJPLHXEKBWMXAMUYALEFQNDLD". puzzle[0][27] is last of first line data 'D'. It is a trash after from puzzle[0][28].
|
0

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.

10 Comments

I'll edit with some more details and some other things you should change.
yea i just changed that, sorry i didnt edit it earlier. check the new updates on the bottom of the post. Instead of that, i just changed it to for cols=0 in the for loop. should get the same thing done. Thanks for taking time to do this!
So did that fix your problem or not? from what I can tell, that's the only problem. Otherwise you're going to have to show me what your text file looks like, because mine is being parsed just fine now.
i added a sample input file in the original post. also from what i see, there are 2 problems that are happening. the line[cols] is skipping a column for each value for some reason, secondly the puzzle[rows][cols]=line[cols] isnt working properly.
Depending on what you mean by skipping a column, it might be because of your use of sizeof. See my edit above. Also it sounds like maybe that second problem is caused by the first ;)
|
-1
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");
    }
}

9 Comments

Is it necessary in addition to the explanation of this cord? Changes:Sikp Spaces.
tried using this code, unfortunately it gives me the same exact outcome.
@Alex I would not be the same result.
@Alec Maybe, you made puzzle[r,c].
@BLUEPIXY should this be in addition to other code (with the exception of the declarations of the variables and files or on its own?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.