How to overwrite or modified record

  • Thread starter Thread starter pajak
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around modifying and adding data to a record file in C++. Participants explore methods for appending data, changing existing data, and deleting data from a file, with a focus on practical coding examples and challenges faced by beginners in programming.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant provides a sample program for inserting data into a file and reading from it, highlighting the use of ofstream and ifstream.
  • Another participant explains that to change data, one must read all data into an array, modify it, and then write it back to the file, as direct modification of specific parts of a file is not possible.
  • A participant expresses confusion about how to implement the suggested changes and requests an example for deleting data from the file.
  • One participant suggests that the original poster may need to learn more about the concepts involved before understanding the provided advice.
  • Another participant proposes using older C-style file handling functions instead of C++ streams, arguing that they may be more transparent and easier to understand for beginners.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to modifying records in a file. There are differing opinions on whether to use C++ streams or C-style file handling, and the discussion reflects a range of understanding among participants.

Contextual Notes

Some participants express uncertainty about specific programming concepts and the implications of different file handling methods. There are also indications of varying levels of experience with programming among the participants.

pajak
Messages
4
Reaction score
0
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:
#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;
}
 

Attachments

Technology news on Phys.org
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.
 
ok..tq sane

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.
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:
#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;
}
 
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.
 
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:
// 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 by a moderator:

Similar threads

Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K