Overwrite and Delete lines from data file
Anyone knows how to overwrite the data in a file ( a single line )
and how to delete the data in a file ( a single line )
Eg.
First, first
Second, second
Third, third
If I want to overwrite line 2 of the file or delete line 2 of the file how should I go about it?
[297 byte] By [
Kel] at [2007-11-18 16:38:02]

# 1 Re: Overwrite and Delete lines from data file
Hi Kel,
I think you need to read the whole file into memory, update the data ( in your case, delete one of the line ), and write the whole thing back again.
Another way is instead of using text file, you can use binary file with a fix length for all your records, by doing so, it is possible to update the record that you are interested in and not need to write the whole file just to update one record
Gnail at 2007-11-11 1:42:01 >

# 2 Re: Overwrite and Delete lines from data file
is there anyway to capture the line for the the data i want to remove or update?
need it v.urgently
Kel at 2007-11-11 1:43:01 >

# 3 Re: Overwrite and Delete lines from data file
you can read your lines of string from file into an array of string, update/delete the string in the array, and write the array of string back to the file.
Gnail at 2007-11-11 1:43:56 >

# 4 Re: Overwrite and Delete lines from data file
hey man watz the problem with u I have seen ur three posting asking same ques. y cant u jst wrk on a single thread:mad:
# 5 Re: Overwrite and Delete lines from data file
Originally posted by Kel
is there anyway to capture the line for the the data i want to remove or update?
need it v.urgently
Here #include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ifstream in("input_file_name",std::ios::binary);
if (!in)
{
std::cout << "problem with file open" << std::endl;
return 0;
}
// determine length of file
in.seekg(0,std::ios::end);
unsigned long length = in.tellg();
in.seekg(0,std::ios::beg);
string str(length,0); // create a string of the appropriate length
// copy file into the string
std::copy( std::istreambuf_iterator<char>(in) ,
std::istreambuf_iterator<char>() ,
str.begin() );
cout << str;
return 0;
}
That code is by Philip Nicoletti
Philip is always nice and kind to everyone on this forum.., he will come to workplace shortly after this(an hour later), I hope he will join help you with some other information...
Regards
# 6 Re: Overwrite and Delete lines from data file
Originally posted by Cyber Bandit
hey man watz the problem with u I have seen ur three posting asking same ques. y cant u jst wrk on a single thread:mad:
C'mon Thomas, are you still a Christian ??
It is neccessary to read the CG rules before posting anything but I think not all the people joining the forums have much time enough to do that, he is new to CG anyway, and any posts like that will be reported to moderators and [(all of them)-1] will be gone some time later...
Cool down, :) Okay ?
# 7 Re: Overwrite and Delete lines from data file
thanks but i need it for my sdi application.
sorry for posting so mani times because i really need it urgently
Kel at 2007-11-11 1:48:02 >
