Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to overwrite or modified record

  1. Dec 19, 2006 #1
    let say i have a simple record file.txt.....how can i modified or add data into the record file?


    here the sample program

    Code (Text):
    #include<iostream>
    #include<string>
    #include <fstream>

    using namespace std;


    class record
    {
        private:


            string input;
            string line;

        public :

            void insertData(){

            ofstream myfile ("example.txt");
            if (myfile.is_open())
            {
                cout <<"Type here : ";
                getline(cin,input);
                //myfile << "This is a line.\n";
                //myfile << "This is another line.\n";
                cout <<"Your has type this word : " << input <<endl;
                myfile <<"Your has type this word : " << input <<endl;
                myfile.close();
            }
            }




            void readData()
            {


            ifstream myfile ("example.txt");
            if (myfile.is_open())
            {
                while (! myfile.eof() )
            {
                getline (myfile,line);
                cout << line << endl;
            }
                myfile.close();
            }

            }

    };








        int main()
    {


        record r1;

        r1.readData();
        r1.insertData();





        system ("pause");

        return 0;
    }
     

    Attached Files:

  2. jcsd
  3. Dec 19, 2006 #2
    To add data, you open the stream to append, as opposed to read, which is what you currently have.

    To change data, you are forced to do all of the work yourself. You have to read in all of the data, parse it to an array. Make the appropriate changes, then save the entire array back to the file, to overwrite the original file.

    You can do this more hastily by writing back to the file whatever you don't need, as you read it. Then once you find what you want to change, make the changes, save it to the file. Then read and write the rest.

    This is all due to the unfortunate (yet understandable) issue that you can not write only to a specific part of a file, you must write the entire file, or nothing at all.
     
  4. Dec 22, 2006 #3
    ok..tq sane

    i'm a very newbie in programming...i really didnt understand how to start...may u show some example


    here the coding i have...n how if i want to delete some data from that file
    Code (Text):

    #include<iostream>
    #include<string>
    #include <fstream>
    #include <iomanip>


    using namespace std;



    class record
    {
        private:




            string input;
            string line;
            string team;
            int win,draw,lost;

        public :






            ofstream myfile ("record.txt",ios::app);
            if (myfile.is_open())
            {
                //cout <<"Type here : ";
                //getline(cin,input);
                cout << "Team name : ";
                getline(cin,team);

                cout << "Win  : ";
                cin  >>win;

                cout << "Draw : ";
                cin  >>draw;

                cout << "Lost : ";
                cin  >>lost;



                cout <<"\n\n";
                cout << "\t\tTeam\t" <<"\t "<< "Win" <<"\t "<< "Draw" <<"\t "<< "Lost"<<endl;
                cout <<"\t\t______________________________________________\n";
                myfile <<"\t\t" << team << "   \t " << win << "\t   " << draw << "\t   " << lost <<endl;
                myfile.close();



            }
            }





            void readData()
            {


            ifstream myfile ("record.txt");
            if (myfile.is_open())
            {
                while (! myfile.eof() )
            {


                getline (myfile,line);
                cout << line << endl;


            }

            }

            }





    };







        int main()
    {


        record r1;




        r1.insertData();
        r1.readData();
       



        system ("pause");

        return 0;
    }



     
     
  5. Dec 22, 2006 #4

    verty

    User Avatar
    Homework Helper

    Pajak, I don't think an example would do much good because it doesn't sound like you know all that you need to. You quoted Sane, you must now learn more until you understand what he meant by that.
     
  6. Dec 29, 2006 #5
    I think the problem here might be iostream... I know that it is generally considered to be quite bad, but use the older functions. This way everything is a bit more transparent, i.e. you can see what is actually happening in your code, as opposed to what you want to have happen. e.g.
    Code (Text):

    // open file
    FILE* f = fopen("somepath/filename.whatever","rb"); // read binary

    // seek to end of file to get length of file, seek back to beginning
    fseek(file, 0L, SEEK_END);
    int lof = (ftell(file))/sizeof(char);
    fseek(file, 0L, SEEK_SET);

    // allocate buffer
    char* buffer = (char*)malloc(sizeof(char)*lof);

    // fill buffer with all of the data from the file
    fread(buffer,sizeof(char),lof,f);

    // null terminate
    buffer[lof-1] = 0;

    fclose(f);
     
    Notice how the functions have more intuitive names and the notation is consistent and nice. For instance, you no longer have to wonder what a stream is, or why << can mean something other than bitshift without being totally ambiguous (these are more advanced C++ concepts imo than function calls and pointers). I use char* and sizeof(char) but there is no reason you can't use fopen with wchar_t* and sizeof(wchar_t) either there is (_)wfopen which takes unicode filenames...


    It may be a bad suggestion, so feel free to ignore it, but I found using these libraries more instructive than using iostream when I was starting with C++, the functions are also documented in more places and used more often. They are included in stdio.h.

    Some excellent reference from msdn: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_stream_i.2f.o.asp
     
    Last edited: Dec 29, 2006
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: How to overwrite or modified record
  1. Record Menu (Replies: 0)

Loading...