How can i edit a file

S

Sanchit

I want to know thta how can i edit a file in C++

For Example my file is

Mr XyZ FFFFFF 65


And now i want go change this number 65 to 87.... how can i Do
this..... I dont want to make a new file but want to edit this file
only.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I want to know thta how can i edit a file in C++

For Example my file is

Mr XyZ FFFFFF 65


And now i want go change this number 65 to 87.... how can i Do
this..... I dont want to make a new file but want to edit this file
only.

Use a fstream to open and read in from the file, then use the same
fstream to write out the changes. Take a look at seekg(), tellg(),
seekp(), and tellp().
 
A

AG

Sanchit said:
A good soul wanted to try his skills on it :

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(void)
{
string buff;
string filename("myfile.txt");
string text1("65");
string text2("87");
fstream file(filename.c_str());

if(!file.good())
{
cout << "Error : cannot open file " << filename << "\n";
return 1;
}
getline(file,buff);
string::size_type pos=buff.find(text1);
if(pos==string::npos)
{
cout << "Error : could not find the string "
cout << text1 << " in file " << filename << "\n";
return 1;
}
buff.replace(buff.find(text1),text2.length(),text2.c_str());
cout << buff << "\n";
file.seekg(0);
file << buff << "\n";
file.close();
return 0;
}

any comment welcome. I could have a done a loop on the getline until end of
file, but ... I am not that kind.

AG.
 
J

James Kanze

On 2007-07-04 08:38, Sanchit wrote:
Use a fstream to open and read in from the file, then use the same
fstream to write out the changes. Take a look at seekg(), tellg(),
seekp(), and tellp().

Be careful, however. You can only replace characters, not
insert or delete. And in a text file, you can only seek to the
results of a previous tell.

Generally speaking, if you write the file with fixed length
lines, and fixed length fields within the file, and always
access it in binary mode, you can edit it pretty effectively.
Otherwise, you're probably better off copying into a temporary
file, making the changes on the fly, and then replacing the
original file with the temporary. (This also has the advantage
of transactional itegrity---you either get all of the changes,
or none, even if the system crashes in the middle of your
operations.)
 
J

James Kanze

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(void)
{
string buff;
string filename("myfile.txt");
string text1("65");
string text2("87");
fstream file(filename.c_str());
if(!file.good())

Just a nit, but this should be:
if ( ! file )
or
if ( ! file.fail() )
(or even
if ( ! file.is_open() )
).

istream::good() has somewhat strange semantics, and I think
(although it's not 100% clear) that it could return false if the
file was empty. (In general, I've never found a use for
istream::good().)
{
cout << "Error : cannot open file " << filename << "\n";
return 1;
}
getline(file,buff);

And of course, you have to check that this succeeded if you
don't want to get into trouble later.
string::size_type pos=buff.find(text1);
if(pos==string::npos)
{
cout << "Error : could not find the string "
cout << text1 << " in file " << filename << "\n";
return 1;
}
buff.replace(buff.find(text1),text2.length(),text2.c_str());
cout << buff << "\n";
file.seekg(0);

Which is valid if, and only if, the change is in the first line.
Otherwise, you have to use a value returned by tellg().
(Logically, since you're going to write, you should probably use
seekp() as well. With a filebuf, it doesn't matter; the put and
get pointers are identical, but it's probably a good habit to
get into.)

Also, there's a very slight chance that getline encountered EOF,
and has set eofbit. If so, the next operation automatically
sets fail state; it's generally a good idea to clear the status
before seeking.
file << buff << "\n";

Note that if the replace changed the length of buff, you're now
in deep trouble.
file.close();
return 0;
}
any comment welcome. I could have a done a loop on the getline
until end of file, but ... I am not that kind.

It would help if we knew more about the context as to why the
original poster wanted to do this. I can think of three
different approaches, depending on the exact goals. If the
replacement string ever has a different length than the
original, he's going to have to copy. Otherwise, if it is a
question of replacing all x with y, where x and y are guaranteed
to have the same length, a loop with getline might be
appropriate, provided you do a tellg before each getline, so you
can successfully seekp to it. And if you really want random
access, you'll have to open the file in binary mode, and it will
probably only work if all of the records (lines) have the same
length.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,163
Latest member
NicolasMcL

Latest Threads

Top