Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.