Hey,
I used getline to read from file.
When i have the char '#' I should ignore that line.. I want to put in the privat values of the class what I got from the file..
Here is what I did.. it works only for one line and then when there is another line it writes the values on top of the first line.

cpp of class Simulation- the function I have problem with

Simulation::Simulation(char *file)
{
    ifstream command(file);
    char str[250];
    char* pch;
    string tempfrom;
    string tempto;
    int i = 0;


    if (command.is_open())
    {

        while (command.getline(str, 250)&& *str!=EOF)
        {
            if (str[0] == '#')
                continue;
            pch = strdup(strtok(str, ","));
            opp= *pch - '0';
            for (i = 0; pch != NULL && i < 3; i++)
            {
                pch = strdup (strtok(NULL, ","));
                if (i == 0)
                {
                    numtaxi = *pch - '0';
                    if (numtaxi != 1 && numtaxi != 2){
                        printToFile("Error - num taxi is not legal");
                        continue;

                    }
                }
                if (i == 1)
                {
                    tempfrom = pch;


                }
                if (i == 2)
                {
                    tempto = pch;
                }
            }

            fromcol = tempfrom[0] - 'a';
            if (!(fromcol < 10 && fromcol>=0)){
                printToFile("Error - from col is not in the range");
                continue;
            }

            fromrow = tempfrom[1] - '0';
            if (!(fromrow < 10 && fromrow>=0)){
                printToFile("Error - from row is not in the range");
                continue;
            }

            tocol = tempto[0] - 'a';
            if (!(tocol < 10 && tocol>=0)){
                printToFile("Error - to col is not in the range");
                continue;
            }

            torow = tempto[1] - '0';
            if (!(torow < 10 && torow>=0)){
                printToFile("Error - to row in not in the range");
                continue;
            }

            setSimulation(getOpp(), getNumtaxi(), getFromcol(), getTocol(), getTocol(), getTorow());

        }

    }
    else
    {
        cout << "unable to open the file";
        exit(1);
    }
    command.close();

}

header of class Simulation

class Simulation{

private:
    int opp;
    int numtaxi;
    int fromrow;
    int fromcol;
    int torow;
    int tocol;

public:
    Simulation(char *file);



    Simulation(int opp1, int numtaxi1, int fromcol1, int fromrow1, int tocol1, int torow1) :opp(opp1), 
        numtaxi(numtaxi1), fromcol(fromcol1), fromrow(fromrow1), tocol(tocol1), torow(torow1){
        cout << "Ctor of Class Simulation is Called" << endl;
    };




    void setSimulation(int opp,int numtaxi, int fromcol, int fromrow, int tocol, int torow);

    int getOpp();
    int getNumtaxi();
    int getFromcol();
    int getFromrow();
    int getTocol();
    int getTorow();
};

`

Since nothing in this code actually writes anything to a file, you should show the code that does.

True.
Wherever you are opening this file, make sure it is opened with the correct append attributes.

This article has been dead for over six months. Start a new discussion instead.