0

what changing i can made in following code that all the previous entered data remain in file even i entered other data!!! means i run program many time so it should contain all data how many times i entered!!!please help...

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>

using namespace std;

void write()
{
    string name; 
    int phone_no; 
    int id; 
    string address;
    string car_name; 
    string car_color; 

    cout << "\n Enter name:";
    cin >> name; 
    cout << "\n Enter phone:";
    cin >> phone_no; 
    cout << "\n Enter id:";
    cin >> id; 
    cout << "\n Enter address:";
    cin >> address; 
    cout << "\n Enter car name:";
    cin >> car_name;
    cout << "\n Enter car model:";
    cin >> car_color; 
    ofstream write_file("car.txt");
    write_file << name << "\n" << phone_no << "\<<id<<"\n;          
    write_file << address << "\n<<car_name<<"\n"<<car_color<<"\n";
    cout << "File written\n";
}

void read()
{
    string name; 
    int phone_no; 
    int id; 
    string address;
    string car_name; 
    string car_color; 

    fstream read_file("car.txt"); 
    read_file >> name >> phone_no >> id >> address >> car_name >> car_color;;
    cout << name << endl << phone_no << endl << id << endl << address << endl;
    cout << car_name << car_color << endl;
}

int main()
{
    int choice;

    do
    {
        cout << "\n*********** Main Menu *************";
        cout << "\n 1: -Do you want to write\n";
        cout << "\n 2: -Do you read\n";
        cout << "\n 3: -exit\n";
        cout << "\n Enter your choice no :";
        cin >> choice;

        switch(choice)
        {
        case 1:
            write();
            break;
        case 2:
            read();
            break;
        case 3:
            exit(0);
            break;
        }

    } while(choice < 3);

    return 0;
    system("pause");
}
ydobonebi
  • 240
  • 2
  • 11
  • 2
    I guess you want to append the file instead of rewriting it right?: std::ofstream outfile; outfile.open("test.txt", std::ios_base::app); outfile << "Data"; –  Jul 25 '15 at 04:58
  • Your question is very unclear. Please try to explain it better or I'm afraid we can't help you. – elixenide Jul 25 '15 at 04:59
  • actually i just want that how many times i called write code it keeps all the records and read code show all the records which i want to see.like first entered record or second and so on... – Momina Idrees Jul 25 '15 at 05:02
  • ok if u can give me the code to keep records in file – Momina Idrees Jul 25 '15 at 05:04
  • I submitted a cleaned up version of your code. I found several syntax errors as a result. I don't know if they exists in your own code. Syntax formatting is very important because it makes syntax errors more seeable. – ydobonebi Jul 25 '15 at 05:31
  • Like the first comment said, it sounds like you want to open the file for appending instead of overwriting. – TheUndeadFish Jul 25 '15 at 05:37
  • Your code has a few syntax errors. Does this even run and if so what errors are you getting? And what you are wanting to do is open a file for appending. You can read with append. You can also just read all the data, add new data and then write out all data (old and new together) which works if you, say, want the infomatin to be sorted in file. – ydobonebi Jul 25 '15 at 06:37
  • Ed Cottrell your edited code works but when run the code again and write data into it, compiler delete the previous data from file write the new one!!! – Momina Idrees Jul 25 '15 at 07:07
  • i want that "compiler contains all the data even program is closed and when again opened and run, file contains all the data which i have entered after reopening file and also that ones which was written first time" – Momina Idrees Jul 25 '15 at 07:14
  • This question will probably be closed as a duplicate soon. If the answers from the duplicates don't fully address your question please [edit] it to explain what makes it unique, then flag it for re-opening. Thanks! – Mogsdad Aug 13 '15 at 19:14

0 Answers0