I am working on this project and i am going to need to write information and retrieve them from a file. I get all the information needed but i am not sure how to write it to a file and how to get it back.

            if (id.is_open())
            {
                cout<<endl;
                cout<<"Enter type: "<<endl;
                cin>>type;
                a.settype(type);
                cout<<"Enter description: "<<endl;
                cin>>description;
                a.setdescription(description);
                cout<<"Enter Counter: "<<endl;
                cin>>counter;
                a.setcounter(counter);
                cout<<"Enter Priority: "<<endl;
                cin>>priority;
                a.setpriority(priority);
                //a.display();
                    b.addInode(a.getincidentid(), a.gettype(), a.getdescription(), a.getcounter(), a.getpriority());
                a.setincidentid(a.getincidentid()+1);
            }

I want to put this information in a file. I am using the linklist which is the addInode. How do i get it into a file and retriveing it from a file. I did some reasearch and i think fstream would do the job. Any help or suggestions would be appreciated.

I should add that i have a file but i am not sure how to get the node in it. After the information is entered into the file i would like to read in back and print out the entire node back to the user.

    void displayInode(int key)  //displays the content of a specific node when searched for
    {
        incident *temp=head;
        while(temp!=NULL)
        {
            if(temp->getincidentid()==key)
            {
                temp->display();
                return;
            }
            temp=temp->getNextincident();
        }
        cout<<"this client record does not exist"<<endl;
    }
    bool isfull()
    {
        incident *cn;
        cn=new incident;

        if(cn==NULL)
            return true;
        else
        {
            delete cn;
            return false;
        }
    }

This is the display note snippet i am going to use it after i pull the ID from the file then print it back to the user.

You can read about input/output with files here http://cplusplus.com/doc/tutorial/files/

In order to store/read information from a file you have to create an object that represents the file.

This is an example of storing data in a file.

The great thing about file objects is that it's treated as a file stream. Operations you're ordinarily used to using with 'cin' and 'cout' like the '<<' and '>>' operators work the same way. You can insert and extract data from a file stream like you do with 'cin' or 'cout'.

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    ofstream output_file;   // create a file for output

    // open the file for output only
    output_file.open("filename_here.txt", ios_base::out);

    if (output_file.is_open()) {    // check if file is opened
        // now let's start inserting data into the file
        output_file << "this is a string\n";
        output_file << "this is another string";
    }

    return 0;
}

Perfect thanks man

Be a part of the DaniWeb community

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