INSERTION


Insertion

In this blog I have explained the insertion() function in detail. Aim of our project is to create the database of the books in the library. We have to maintain all the records and details of all the books. 
So the important thing is to insert the data into the file in well structured format. 
To do this we have check first that the file is opened successfully or not.For that we put condition in if loop 'file.is_open()'. This will return true if file is opened successfully otherwise it will return false.
Once it enters in if loop it will take all the inputs from the user like name of the book, name of the author, publication, number of pages etc.


void insertion()
    {
 fstream file;
        file.open("cp.txt",ios::out|ios::app);
        books b1;
Function starts with creating the object of fstream class. As we have to write in the file we have to open it and also we have to append the data in it. So the next line we used the file.open() function. The parameters ios::out |ios::app  make file to open in write mode and append mode.  Book b1 is structure.


 if(file.is_open())
          {
            cout<<endl<<"File opened succesfully ";
            cout<<endl<<"\nEnter the name of the book ";
            cin>>b1.name;
            cout<<"Enter the name of the author ";
            cin>>b1.author;
            cout<<"Enter the name of the publication ";
            cin>>b1.publication;
            cout<<"Enter the number of pages ";
            cin>>b1.pages;
            cout<<"Enter the count of the books ";
            cin>>b1.counti;
            //cout<<b1.counti;
            file<<"\n\n"<<"BOOK "<<number++;
            file<<"\n";

These are the simple lines of code that are just taking the input from the user.
 After taking all inputs successfully  next step is to insert data in structured format.

            file.fill(' ');
            file.setf(ios::left,ios::adjustfield);
            file.width(20);
            file<<"name"<<b1.name<<endl;

            file.fill(' ');

            file.setf(ios::left,ios::adjustfield);
            file.width(20);
            file<<"author"<<b1.author<<endl;

            file.fill(' ');

            file.setf(ios::left,ios::adjustfield);
            file.width(20);
            file<<"publication"<<b1.publication<<endl;

            file.fill(' ');

            file.setf(ios::left,ios::adjustfield);
            file.width(20);
            file<<"pages"<<b1.pages<<endl;

            file.fill(' ');

            file.setf(ios::left,ios::adjustfield);
            file.width(20);
            //cout<<b1.counti;
            //fflush(stdin);
            file<<"count"<<b1.counti<<endl;

          file.close();

          copying();
          writing();
          }
       else
         cout<<endl<<"Error in opening the file";

    }


file.setf(ios::left,ios::adjustfield)  is used to write a data in the file in specific format. It forces text in fixed width field to be output with left justification.

The field width determines the minimum number of characters to be written in some output representations. If the standard width of the representation is shorter than the field width, the representation is padded with fill characters at a point determined by the format flag adjustfield (one of leftright or internal).

After inserting all the data file is closed using function file.close().
In case if file is not opened it will go into the else loop and print the message "Error in opening the file"


                                                       Before inserting the data

                                        
                                                      After inserting the data

Comments

Post a Comment

Popular posts from this blog

SEARCHING

ORDER BY PART-2

GROUP BY