i have this code uptil yet in which i am adding records in a text file [array of structure] and displaying the text file
what i want to do is to implement delete previous record functionality and also to edit the previous records
could anyone please guide me as to how to do that
any help will be helpful .. Thanks

 #include<iostream>
    using namespace std;
    #include<conio.h>
    #include<fstream>
    #include<iomanip>
    #include<stdlib.h>
    struct patient{
        int pid;
        string pname;
    };

    int enterChoice(void);
    void displayFile();
    void updateRecord();
    void newRecord();
    void deleteRecord();
    void outputLine();

    int enterChoice(void){
    cout<<endl<<"ENTER YOUR CHOICE"<<endl
        <<"1.) Display File"<<endl
        <<"2.) update a patient record"<<endl
        <<"3.) add a new record"<<endl
        <<"4.) delete a record"<<endl
        <<"5.) end program"<<endl;
        int menuchoice;
        cin>>menuchoice;
        return menuchoice;  

    }
    void displayFile(){
    string line;
    ifstream ifile;
    ifile.open("patient.txt",ios::in);
    if(ifile.is_open())
    {
    while(getline(ifile,line))
    {
    cout<<line<<endl;

    }
    ifile.close();
    }
    }














    void updateRecord(){


    }
    void newRecord(){


    ofstream outf("example.txt",ios::out);
        if(!outf)
    {
        cerr<<"Sample.dat cannot be opened for writing"<<endl;
        exit(1);
    }






    }

    void deleteRecord(){


    }

    void outputLine(){



    }





    int main()
    {

                int size;
             cout<<"Enter Number Of Patients To Save";
             cin>>size;
            patient a[size];
            ofstream outf("patient.txt",ios::out);  
            if(!outf)
            {
                cerr<<"Sample.dat cannot be opened for writing"<<endl;
            exit(1);
            }   
                int i;
        for(i=0;i<size;i++)
        {
        cout<<"Enter Patient id for patient "<<i<<endl;
        cin>>a[i].pid;
        cout<<"Enter Patient name for patient "<<i<<endl;
        cin>>a[i].pname;    

        }




        int j;


        for(j=0;j<size;j++)
        {

        outf<<a[j].pid<<a[j].pname<<";"<<endl;  

        }
        outf.close();



        //menu for making choice
        int choice;
        while((choice = enterChoice() ) !=5 ){

        switch(choice){
            case 1:
                displayFile();
                break;
            case 2:
                updateRecord();
                break;
            case 3:


            break;
            case 4:
                deleteRecord();
                break;
            default:
                cerr<<"Incorrect Choice"<<endl;
                break;
        }


        }

        getch();
        return 0;
    }

Recommended Answers

All 2 Replies

Long story short, you can't alter the text file. You can add new things to the end of the text file, but if you want to delete parts of it or change it, you can't. You have to create a whole new text file and delete the old one.

For structures of equal size you could try an indexed sequential file. Google.

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.