C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Reading from file. Posted by daviddoria on 18 Nov 2006 at 10:26 AM
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 \0 terminator

}

fclose(instream);

OutputMaze(maze,12,12);

return Size;

}
Report
Re: Reading from file. Posted by stephl on 19 Nov 2006 at 4:49 AM
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); 
/*
InputChar is an array so the '&' before it is not necessary.
*/

maze[row][col]=InputChar;
/*
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];
*/
} 

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

} 

fclose(instream); 

OutputMaze(maze,12,12); 

return Size; 

}


Steph



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.