Reading from file. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Reading from file.

daviddoriadaviddoria Posts: 1Member
I'm having a huge issue getting simple character matrix from a file.

I have this file: maze.txt

bxbbxbbbbxbb
bbbbbbxxxbxb
bxbbbxbbbxbb
bbxbbbbxbbxb
bxxxxbxbbxbb
bxbbbbbbxbbb
bxbbxxbxbbxb
bxxbxbbxbxxb
bxbxbbxbbxbb
xbbbbbbbbxbb
bbbxxxbbbbxb
bxbxxxxbbxbb

and i just want to read it into a 2d array of chars. I have a working output function, and when i output the result of this function, it is simply garbage. Anyone see what's wrong??

Thanks,

David

struct COORD InputMaze(int *maze[][101])
{

int row;
int col;
char InputChar[1];
struct COORD Size;

FILE *instream;

instream=fopen("maze.txt","r");

/*
fscanf(instream,"%i",&Size.row);
fscanf(instream,"%i",&Size.col);
*/

for(row=1;row<=12;row++)
{
for(col=1;col<=12;col++)
{

fscanf(instream, "%c", &InputChar);

maze[row][col]=InputChar;
}

InputChar[0]=fgetc(instream);//get rif of the terminator

}

fclose(instream);

OutputMaze(maze,12,12);

return Size;

}

Comments

  • stephlstephl Posts: 422Member
    [code]
    struct COORD InputMaze(int *maze[][101])
    {

    int row;
    int col;
    char InputChar[1];
    struct COORD Size;

    FILE *instream;

    instream=fopen("maze.txt","r");

    /*
    fscanf(instream,"%i",&Size.row);
    fscanf(instream,"%i",&Size.col);
    */

    for(row=1;row<=12;row++)
    {
    for(col=1;col<=12;col++)
    {

    fscanf(instream, "%c", &InputChar);
    /*[red]
    InputChar is an array so the '&' before it is not necessary.
    [/red]*/

    maze[row][col]=InputChar;
    /*[red]
    Type error. The left handside is (int *) while the right one is (char *).
    I think maze should be declared:
    int maze[][101];
    Then you can write:
    maze[row][col]=InputChar[0];
    [/red]*/
    }

    InputChar[0]=fgetc(instream);//get rif of the [red]
    [/red] terminator

    }

    fclose(instream);

    OutputMaze(maze,12,12);

    return Size;

    }
    [/code]

    Steph
Sign In or Register to comment.