I believe that ofstream is for writing into a file and ifstream is for reading from a file.

I am trying to write into a file by taking commandline input from the user which is the accept_data() function and and then display all the data from the file that the user entered using display_data() function. I am not sure how to get the input from the commandline from the user.
Here's my code in C++

#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;

class travels {

        private:

                string customername[20];
                int no_of_people;
                char package_category;
                float cost;
                string date[8];

        public:

                void accept_data();
//              void display_data();


};


void travels :: accept_data() {

        ofstream ofile;
        ofile.open("CUSTFILE.DAT", ios::out | ios::binary);





}

 /*void travels :: display_data() {

        ifstream Ifile;
        Ifile.open("CUSTFILE.DAT");

        while(Ifile) {

                Ifile.get(customername);
                cout<<customername;

        }
}*/


int main() {

        travels t;

       t.accept_data();

        //t.display_data();
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

In the accept data function, write a bunch of cin statements and when the user enters the data, write all of it to the file.

I am not sure how to get the input from the commandline from the user.

Do you want the user to type something in the console? In that case you get 'get' this input with "cin", it works simmilarly to cout.

If you want a user to start your application with some arguments( "MyApplication.exe Some Input Here" ) you need to modify your main function a little:

int main( int argc, char** argv )
{
    for( int i = 0; i < argc; ++i )
        cout << "Argument "<<i<<": "<<argv[i]<<"\n";

}

The arguments will be stored in argv, and the number of arguments that are stored is set in argc (not that argv[0] will be your Executable name, so MyApplication.exe)

Edited 6 Years Ago by thelamb: n/a

Hi Folks:

Thank you for your replies. Yes, I am trying to write into a file and then read it. The writing part works now. The code for reading the contents in the file is in display_data() function. The code compiles but it does not display the contents. Can you please take a look at this :

#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;

class travels {

        private:

                string customername;
                int no_of_people;
                char package_category;
                float cost;
                string date;

        public:

                void accept_data();
                void display_data();


};


void travels :: accept_data() {

        ofstream ofile;
        ofile.open("CUSTFILE.DAT", ios::out | ios::binary | ios::app);

        getline(cin,customername);
        cin>>no_of_people;
        cin>>package_category;
        cin>>cost;
        cin>>date;

        ofile<<customername<<" "<<no_of_people<<" "<<package_category<<" "<<cost<<" "<<date<<endl;

        ofile.close();


}

 void travels :: display_data() {

        ifstream Ifile;
        Ifile.open("CUSTFILE.DAT");

        while(!Ifile.eof()) {

                Ifile>>customername;

        }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The problem is that you are reading the data into one variable. You need to read in the variables how you outputed then.

Ifile >> customername >> no_of_people >> package_catagory >> ...

You can also write and read the entier class since you are using binary output like

Ofile.write((char*) this, sizeof this);  // this writes the whole class
Ifile.read((char*) this, sizeof this);  // this reads the whole class

Edited 6 Years Ago by NathanOliver: n/a

Hi NathanOliver:

I thought write() and read() are for integers while get() and put() are for characters

This is what I have.. Any record i input is sent to file but on the screen it keeps printing it for ever.. why does that happen ? Instead of just printing the contents of the file, it keeps printing the record I entered over and over ..

void travels :: accept_data() {

        ofstream ofile;
        ofile.open("CUSTFILE.DAT", ios::out | ios::binary | ios::app);

        getline(cin,customername);
        cin>>no_of_people;
        cin>>package_category;
        cin>>cost;
        cin>>date;

        ofile<<customername<<" "<<no_of_people<<" "<<package_category<<" "<<cost<<" "<<date<<endl;

        ofile.close();


}

 void travels :: display_data() {

        ifstream Ifile;
        Ifile.open("CUSTFILE.DAT");

        while (!Ifile.eof()) {

        //      Ifile>>customername;
                cout<<customername;
        //      Ifile>>no_of_people;
                cout<<no_of_people;
        //      Ifile>>package_category;
                cout<<package_category;
        //      Ifile>>cost;
                cout<<cost;
        //      Ifile>>date;
                cout<<date;
        }
}

Write and read are defined as taking a char * as there first paramater and the amount of bytes to read as the second paramater. Because it use a char pointer we can effectivly cast any type of data we want as long as we get the size. So if you want to write an entire class to a file you caan use write and read as how I showed in my previous post. This does not work with dynamic contianers like a string or a vectore since there sixe can change. You need to add an extra step in these cases. Were you able to get your values from the file using my first method?

The ios::binary flag might be interfering with outputing a string. Also you don't need a while loop since there is only one set of data. I would just get rid of the while loop for now.

Edited 6 Years Ago by NathanOliver: n/a

When I say Ifile.close() it works better instead of printing the last entered data over and over. Instead, after using the Ifile.close(), it displays only the last entered record on the screen. Why does it not enter all the data from the file and instead displays the last entered record ?

void travels :: display_data() {

        ifstream Ifile;
        Ifile.open("CUSTFILE.DAT");

        while (Ifile) {

                Ifile>>customername;
                cout<<customername;
                Ifile>>no_of_people;
                cout<<no_of_people;
                Ifile>>package_category;
                cout<<package_category;
                Ifile>>cost;
                cout<<cost;
                Ifile>>date;
                cout<<date;

                Ifile.close();
        }
}

try storing all the customer info to some string variable like buffer and then write the buffer into the file

the display function of urs is not reading from CUSFILE.DAT. it only displays the last data that u hav entered. run the program, without entring the data run display function
u wil see nothing

Edited 6 Years Ago by debugger09: n/a

void_travels :: display_data()
{

ifstream Ifile;
string buffer;

Ifile.open("CUSTFILE.DAT",ios::out);

while (!Ifile.eof())
{
buffer.erase();
getline(Ifile,buffer);
cout<<buffer;
cout<<"\n";
}
Ifile.close()
}

try using this code to display

Edited 6 Years Ago by debugger09: n/a

void travels :: accept_data()
{
string buffer;
ofstream ofile;
ofile.open("CUSTFILE.DAT", ios::out | ios::binary | ios::app);

getline(cin,customername);
cin>>no_of_people;
cin>>package_category;
cin>>cost;
cin>>date;

buffer+=customername+"|"+no_of_people+"|"+package_category+"|"+cost+"\n";

ofile<<buffer;

ofile.close();


}

use this code to read the data and try

Edited 6 Years Ago by debugger09: n/a

Hi Debugger: Why do I need a buffer ?

did u try to run the code that i posted

is it working??

i hav not tested it

You don't and what he entered will not work since all of your members are not strings.

ohh yes u r right

srry i dint completely go through his program

but he can change all his fields to buffer

Edited 6 Years Ago by debugger09: n/a

Hi Debugger: Why do I need a buffer ?

buffer is just need put all your fields together as a single string and then write it as a complete line to the file

while displaying data it reads one complete line from file and store it on buffer and then print

note that all ur fields must be of string type

If someone can help me understand what's going on, it would be greatly appreciated!

Try this

void travels :: display_data() 
{
    ifstream Ifile;
    Ifile.open("CUSTFILE.DAT");
    Ifile >> customername >> no_of_people >> package_category >> cost >> date;
    cout << customername << " " << no_of_people << " " << package_category << " " << cost << " " << date;
}

Edited 6 Years Ago by NathanOliver: n/a

Hi NathanOliver:

I implemented the above code but still got only the last entered record. Not the entire file! :(

No, its not array of objects,

multiple input records just like the above ones stored in it

Why do you have multiple records in one file if the member variables of the class are not arrays. If they are arrays then you should use a while loop like you had and add a counter so you can input the values in the right index.

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