How to overwrite or modified record

  • Thread starter pajak
  • Start date
In summary, to modify or add data into a record file, you will need to open a stream to append or overwrite the file. To change data, you will need to read in all of the data, make the appropriate changes, and then save the entire array back to the file. It may be helpful to use the older functions in the stdio.h library for a more transparent and consistent approach to file manipulation.
  • #1
pajak
4
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

  • example.txt
    95 bytes · Views: 527
Technology news on Phys.org
  • #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.
 
  • #3
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;
}
 
  • #4
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.
 
  • #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:
// 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:

FAQ: How to overwrite or modified record

How can I overwrite a record in a database?

To overwrite a record in a database, you will need to use an SQL UPDATE statement. This statement allows you to specify which record you want to update and which values you want to change. Make sure to include a WHERE clause to ensure you are only updating the specific record you want.

Can I modify a record without changing the original data?

Yes, you can modify a record without changing the original data by using an SQL UPDATE statement with the SET clause. This allows you to specify which columns you want to update and what values you want to change them to, while keeping the rest of the data in the record unchanged.

How do I ensure that the modified record is saved in the database?

To ensure that the modified record is saved in the database, you will need to use an SQL COMMIT statement. This statement will commit any changes made to the database since the last COMMIT or ROLLBACK statement. Make sure to use this statement after any modifications to ensure they are saved.

Is it possible to undo a record modification?

Yes, it is possible to undo a record modification by using an SQL ROLLBACK statement. This statement will roll back any changes made since the last COMMIT statement, essentially undoing the modification to the record. Keep in mind that this will also undo any other changes made to the database since the last COMMIT.

Can I modify multiple records at once?

Yes, you can modify multiple records at once by using an SQL UPDATE statement with a WHERE clause. This allows you to specify certain criteria that the records must meet in order for the modification to be applied. You can also use the SET clause to specify which columns and values you want to modify in the selected records.

Similar threads

Replies
8
Views
6K
Replies
4
Views
2K
Replies
7
Views
1K
Replies
5
Views
2K
Replies
6
Views
1K
Replies
1
Views
6K
Replies
5
Views
2K
Replies
9
Views
3K
Back
Top