## how to overwrite or modified record

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();
}
}

{

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

}

};

int main()
{

record r1;

r1.insertData();

system ("pause");

return 0;
}
Attached Files
 example.txt (95 Bytes, 0 views)
 PhysOrg.com science news on PhysOrg.com >> King Richard III found in 'untidy lozenge-shaped grave'>> Google Drive sports new view and scan enhancements>> Researcher admits mistakes in stem cell study
 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();

}
}

{

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

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

}

}

}

};

int main()
{

record r1;

r1.insertData();

system ("pause");

return 0;
}

## how to overwrite or modified record

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/de...eam_i.2f.o.asp