I\'m trying to read in 2 seperate text files using ifsteam. Thefirst file is an
ID: 3609104 • Letter: I
Question
I'm trying to read in 2 seperate text files using ifsteam. Thefirst file is an instruction set, the second is the maze. Here iswhat I have, but only the first instance of ifstream will open thefile. I've tried using the same instance of ifsteam to open thesecond file, but it still only opens the first, no matter whichorder they are in. Any help would be greatly appreciated.ThanksFigured it out. I had a cin.get() waiting for auser input before the program would continue. It's been a whilesince i've programmed.
int maze[63];//single dimension array to represent maze
/******This is where I read in the MAZEfrom maze.txt*********/
ifstream mazefile("maze.txt");
if (mazefile.is_open())
{
int max_read = 64;//only 64 spaces in array, so only need to read 64 values
int amountRead =0;//counter to see how many values read
while (! mazefile.eof())
{
while(mazefile>>maze[amountRead]&& amountRead <max_read)
{
amountRead++;
}
for(int i =0; i < max_read; i++)
{
cout << maze[i];
if((i%8) == 7)
{
cout << endl;
}
}
}
mazefile.close();
}
else
{
cout << "Unable to open file";
}
mazefile.clear();
int instructions[24]; //should befewer than 25 instructions, 5's mean no more instructions
for(int i=0; i<25; i++)
{
instructions[i]=5;
}
/***********This is where I readinstructions from instructions.txt**********/
ifstream myfile("instructions.txt");
if (myfile.is_open())
{
int max_read = 25;//only 25 spaces in array, so only need to read first 25 values
int amountRead =0;//counter to see how many values read
int i = 0;
while (! myfile.eof())
{
while(myfile>>instructions[amountRead]&& amountRead< max_read)
{
amountRead++;
}
for(i = 0; i< max_read; i++)
{
cout << instructions[i] << endl;
}
}
myfile.close();
}
else
{
cout << "Unable to open file";
}
myfile.clear();
Explanation / Answer
/*
* Prototypes ---place the prototypes for your user definedfunctions here
*/
int main()
{
int maze[63];//single dimension array torepresent maze
/******This is where I read in the MAZEfrom maze.txt*********/
ifstream mazefile("maze.txt");
if (mazefile.is_open())
{
cout<<" Contents of maze.txtare :";
int max_read = 64;//only 64 spaces in array, so only need to read 64 values
int amountRead =0;//counter to see how many values read
if (! mazefile.eof())
{
while(mazefile>>maze[amountRead]&& amountRead <max_read)
{
amountRead++;
if(mazefile.eof())
break;
}
mazefile.close();
}
for(int i = 0; i < amountRead; i++)
{
cout << maze[i] <<" ";
if((i%8) == 7)
{
cout << endl;
}
}
}
else
{
cout << "Unable to open file";
}
mazefile.clear();
int instructions[24]; //should befewer than 25 instructions, 5's mean no more instructions
for(int i=0; i<25; i++)
{
instructions[i]=5;
}
/***********This is where I readinstructions from instructions.txt**********/
ifstream myfile("instructions.txt");
if (myfile.is_open())
{
int max_read = 25;//only 25 spaces in array, so only need to read first 25 values
int amountRead =0;//counter to see how many values read
int i = 0;
if (! myfile.eof() )
{
cout<<" Contents ofinstructions.txt are :";
while(myfile>>instructions[amountRead]&& amountRead< max_read)
{
amountRead++;
if(myfile.eof())
break;
}
myfile.close();
for(i = 0; i< amountRead; i++)
{
cout << instructions[i] << endl;
}
}
}
else
{
cout << "Unable to open file";
}
myfile.clear();
system("pause");
}