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] == '#')
            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");

                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");

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

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

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

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


        cout << "unable to open the file";


header of class Simulation

class Simulation{

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

    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();


Recommended Answers

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

Jump to Post

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.