#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream dataFile;
string filename, line , lineTesting1;
char characterDrawing , symbol;
int speed, x, y , boardSizeX , boardSizeY , numberOfLine = 0;
string phrase, direction , wrapAround , rotation;
cout<<"Please input filename: ";
getline(cin, filename);
dataFile.open(filename);
if (dataFile)
{
while (getline(dataFile,line))
{
numberOfLine += 1;
}
dataFile.close();
if (numberOfLine == 6)
{
dataFile.open(filename);
dataFile >> characterDrawing;
dataFile.ignore(20,'\n');
if (isspace(characterDrawing))
{
symbol = '#';
}
else
{
symbol = characterDrawing;
}
dataFile >> speed;
dataFile.ignore(20,'\n');
getline(dataFile,phrase);
dataFile >> x >> y;
dataFile.ignore(20,'\n');
dataFile >> boardSizeX >> boardSizeY;
dataFile.ignore(20,'\n');
dataFile >> direction >> wrapAround >> rotation;
cout << symbol << endl;
cout << speed << endl;
cout<< phrase<<endl;
cout << x << " " << y<<endl;
cout << boardSizeX << " "<< boardSizeY<<endl;
cout << direction << wrapAround << rotation<<endl;
}
if (numberOfLine == 5)
{
dataFile.open(filename);
for (int i=0; i<3 ; i++)
{
getline(dataFile,lineTesting1);
}
if (dataFile.fail())
{
dataFile.close();
dataFile.open(filename);
symbol = '#';
dataFile >> speed;
dataFile.ignore(20,'\n');
getline(dataFile,phrase);
dataFile >> x >> y;
dataFile.ignore(20,'\n');
dataFile >> boardSizeX >> boardSizeY;
dataFile.ignore(20,'\n');
dataFile >> direction >> wrapAround >> rotation;
cout << symbol << endl;
cout << speed << endl;
cout<< phrase<<endl;
cout << x << " " << y<<endl;
cout << boardSizeX << " "<< boardSizeY<<endl;
cout << direction << wrapAround << rotation<<endl;
}
}
else
{
cout<<"Error opening the file.\n";
}
dataFile.close();
return 0;
}
}
What I have tried:
The first problem that I encountered before is solved. New problem is when I add in code
if (numberOfLine == 5)
{
dataFile.open(filename);
for (int i=0; i<3 ; i++)
{
getline(dataFile,lineTesting1);
}
if (dataFile.fail())
{
dataFile.close();
dataFile.open(filename);
symbol = '#';
dataFile >> speed;
dataFile.ignore(20,'\n');
getline(dataFile,phrase);
dataFile >> x >> y;
dataFile.ignore(20,'\n');
dataFile >> boardSizeX >> boardSizeY;
dataFile.ignore(20,'\n');
dataFile >> direction >> wrapAround >> rotation;
cout << symbol << endl;
cout << speed << endl;
cout<< phrase<<endl;
cout << x << " " << y<<endl;
cout << boardSizeX << " "<< boardSizeY<<endl;
cout << direction << wrapAround << rotation<<endl;
}
}
Its output is Error opening the file while the file is actually exists. The debugger do not has any error for me.
My data is as follow :
10
Pizza Is Love
10 28
30 67
lr wt 180
while first one is is supposed any symbol but here the file did not mention and it will set to default.