Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#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
C++
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.
Posted
Updated 21-Sep-19 1:48am
v2
Comments
Richard Deeming 20-Sep-19 14:31pm    
"Error opening the file"
"The debugger do not has any error for me"

Which is it - there's an error, or there's NOT an error?
Member 14598404 20-Sep-19 22:16pm    
In my code has mentioned if do not have this such file , the program will output Error opening the file and what is happening now. The debugger do not have any error message for me.
Richard MacCutchan 21-Sep-19 3:20am    
Yes, but what is the error? We cannot see your screen or guess what is happening.
Member 14598404 21-Sep-19 4:11am    
There is no error message the debugger gives.
The problem is the file cannot found so the program print output Error opening the file that I code in my program (print out when no such files)
(THE FILE IS EXIST ACTUALLY)
Richard MacCutchan 21-Sep-19 9:08am    
The file may exist, but the name you are using in your program does not. Make use of the debugger to find out why it is failing.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900