This is in C, I have no idea how to do it. How do I read a file? What include\'s
ID: 3670061 • Letter: T
Question
This is in C, I have no idea how to do it. How do I read a file? What include's do I need? I basically know nothing of how to do this.
Code a program and reads a maze file and exits non-zero if the maze is not valid. Or outputs the maze in a frame if it is good.
The name of the file containg the maze is the only command-line parameter to this program. Read that file with the stdio functions, and close it propperly.
A maze is a rectangular array of characters. It is encoded in the file in 2 parts:
header line
The header has 2 decimal intergers separated by white-space (spaces, or tabs).
The first is the number of collums, a literal comma ( ,) then the number of rows. For example a maze with 10 collums per line, and 8 rows would start with a line:
10, 8
There could be leading white-space, but fscanf(3) will do that for you. I put in the newline so you could see it.
A maze with either dimension < 2 is not a proper maze, and you should output an error and exit with 2 in that case (you don't have to read the rest of the file, but you can if you like).
Maze rows
The maze has implied walls around it. So a space on the edge is passable, but you can't leave the rectangle. Each row represents a map of the maze within that larger box:
a space is part of a passage
an octothorpe ( #) is a wall.
the letter s is the start of the maze
The player is dropped on this square from above.
the g charater is the goal
The player escapes to the next level down, or out from this position
Requirements - maze part 1
I would do them in the order presented.
Read the maze into memory you allocate.
You may put it in an array of pointers to chars, or an array of pointers to an array of a stuct you define. In the next assignment you might need the struct. Still your choice.
You have to exit non-zero on any of these conditions
Oh, and a nifty error to stderr would be implied here. And a unique exit code.
you could not open the maze file
you ran out of memmory to hold the maze
the maze is to small (either dimension < 2)
the maze has a total start cells that is not 1
the maze has no goal cell
you ran out of row or column characters
a cell has any other character in it (yes even a tab)
Is it not an error to have extra data on the end of the file. We might read more from the file in another assignent, so don't close it in the reader function. #clue
Only if the maze is good, then print it back out
You don't have to output the numbers, just the maze in a frame.
box in the maze with --- (dash) bars on the top and bottom
show the edges with | (pipe) characts on the left and right edges
Explanation / Answer
#include<stdio.h>
#include<fstream.h>
void main()
{
char m[2][2],s,g,i,=0,j=0;
void readdata()
{
fprintf(" enter mage of an image );
fscanf("%c", &m);
}
void display()
{
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
if(s=='---')
fprintf(" the starting of a mage is", %s);
}
elseif(g=='|')
fprintf(" the edge of a mage is", %g);
else
fprintf(" error");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.