| Thread Closed |
how to overwrite or modified record |
Share Thread | Thread Tools |
| Dec19-06, 09:08 AM | #1 |
|
|
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();
}
}
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;
}
|
| Dec19-06, 05:42 PM | #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. |
| Dec22-06, 06:09 AM | #3 |
|
|
ok..tq sane
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;
}
|
| Dec22-06, 06:40 AM | #4 |
|
|
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.
|
| Dec29-06, 06:05 AM | #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);
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 |
| Thread Closed |
| Thread Tools | |
Similar Threads for: how to overwrite or modified record
|
||||
| Thread | Forum | Replies | ||
| anyone can help to modified this program? | Programming & Comp Sci | 5 | ||
| Question about a modified 747 | Mechanical Engineering | 2 | ||
| Modified Atwood Help | Introductory Physics Homework | 6 | ||
| Modified Ampere's law | Introductory Physics Homework | 5 | ||
| Scientists record new internet speed record | Computing & Technology | 11 | ||