1,105,456 Community Members

FILE HANDLING

Member Avatar
aslam.junaid786
Newbie Poster
12 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
        CODE



                    #include "stdafx.h"
                    #include<iostream>
                    #include<conio.h>
                    #include<string>
                    #include<fstream>
                    using namespace std;
                    //---------------------------Function Prototypes----------------------------//
                    void titleheader();
                    void menu();
                    void addbook();
                    void display();
                    //--------------------------Main Function---------------------------------//
                    int _tmain(int argc, _TCHAR* argv[])
                    {
                        int op;
                        titleheader();
                        menu();
                        cout<<"\nPress option key to activate the required function: ";
                        cin>>op;
                        switch (op)
                        {
                        case 1:
                            {
                                system("cls");
                                titleheader();
                                cout<<"\n";
                                addbook();  
                                system ("pause");
                                cin.get();
                                system("cls");
                                break;
                            }
                        case 2:
                            {
                                system("cls");
                                titleheader();
                                display();
                                system ("pause");
                                cin.get();
                                system("cls");
                                break;
                            }
                        default :
                            {
                                exit (0);
                            }
                        }
                        getch();
                    }
                    //************************************ END of main() and Functions Bodies are given below. *********************************************\\
                    void titleheader()
                    {
                        cout<<"\n ------------------------------------------------------------------------------ "<<endl;
                        cout<<"                      **** Book Store Managment ****                             "<<endl;
                        cout<<" ------------------------------------------------------------------------------\n"<<endl;
                    }
                    void menu()
                    {
                        cout<<"1). Add a New Book. "<<endl;
                        cout<<"2). Display all Records. "<<endl; 
                    }
                    void addbook()
                    {
                        string bookname;
                        string publisher;
                        string author;
                        int copies;
                        int price;
                        int edition;
                        ofstream input("abc.txt",ios::app);
                        cout<<"Enter Book name: ";
                        cin>>bookname;
                        cout<<"Enter Book Publisher name: ";
                        cin>>publisher;
                        cout<<"Enter Author/Writer name: ";
                        cin>>author;
                        cout<<"Enter Number of Copies : ";
                        cin>>copies;
                        cout<<"Enter Price: ";
                        cin>>price;
                        cout<<"Enter Edition: " ;
                        cin>>edition;
                        input<<bookname<<publisher<<author<<copies<<price<<edition;
                        input.close();
                    }
                    void display()
                    {
                        ifstream ofile("abc.txt");
                        string bookname;
                        string publisher;
                        string author;
                        int copies;
                        int price;
                        int edition;
                        cout<<" Book Name: "<<bookname<<endl;
                        cout<<" Publisher Name: "<<publisher<<endl;
                        cout<<" Author Name : "<<author<<endl;
                        cout<<" No. Of Copies: "<<copies<<endl;
                        cout<<" Price: "<<price<<endl;
                        cout<<" Book Edition: "<<edition<<endl;
                        while (ofile>>bookname>>publisher>>author>>copies>>price>>edition)
                        {
                            cout<<bookname<<publisher<<author<<copies<<price<<edition;
                        }
                        ofile.close();
                    }
            
        


> >     In display Function Ouput comes: 
> 
> 
>    * Book Name:
>     Publisher Name:
>     Author Name :
>     No. Of Copies: 0
>     Price: 0
>     Book Edition: 0*

File is saved in project folder and data is written on it !! but don't know why Display function file not working..I don't Know much about file handling. I made this program with the help of a youtube tutorial:
https://www.youtube.com/watchv=p3m3qLNX9zA (https://www.youtube.com/watch?v=p3m3qLNX9zA) ..The purpose of this program is to ADD data and DISPLAY data by using file handling !! Please help me with it !!


    
    
Member Avatar
Ancient Dragon
Achieved Level 70
27,668 posts since Aug 2005
Reputation Points: 5,234 [?]
Q&As Helped to Solve: 3,040 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

Post the addbook() function.

Member Avatar
nullptr
Posting Whiz in Training
255 posts since Mar 2012
Reputation Points: 49 [?]
Q&As Helped to Solve: 50 [?]
Skill Endorsements: 2 [?]
 
0
 

See the comments I added to your display function.

void display()
{
    ifstream ofile("abc.txt");
    string bookname;
    string publisher;
    string author;
    int copies;
    int price;
    int edition;

    // none of this will display anything from abc.txt
    // as nothing from abc.txt has been read.
    cout<<" Book Name: "<<bookname<<endl;
    cout<<" Publisher Name: "<<publisher<<endl;
    cout<<" Author Name : "<<author<<endl;
    cout<<" No. Of Copies: "<<copies<<endl;
    cout<<" Price: "<<price<<endl;
    cout<<" Book Edition: "<<edition<<endl;


    // this is where you read abc.txt, but you're not formatting the output
    // so it will display as one contiguous jumble of book details
    while (ofile>>bookname>>publisher>>author>>copies>>price>>edition)
    {
        cout<<bookname<<publisher<<author<<copies<<price<<edition;
    }
    ofile.close();
}

Just move the formatting to inside the while loop.

    while (ofile>>bookname>>publisher>>author>>copies>>price>>edition)
    {
        cout<<" Book Name: "<<bookname<<endl;
        cout<<" Publisher Name: "<<publisher<<endl;
        cout<<" Author Name : "<<author<<endl;
        cout<<" No. Of Copies: "<<copies<<endl;
        cout<<" Price: "<<price<<endl;
        cout<<" Book Edition: "<<edition<<endl<<endl;
    }

It would also be a good idea to check whether abc.txt is actually opened.

    ifstream ofile("abc.txt");
    if (!ofile.is_open() )
    {
        // error opening file, handle error
        return;
    }
Member Avatar
aslam.junaid786
Newbie Poster
12 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thankx Sir for a great reply.. I have done what you said but when I go fro Displaying Output it say " PRESS ANY KEY TO GO BACK.." ..the file is not showing saved data on display function call !!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article