By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

How can i edit a file

P: n/a
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.

Jul 4 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2007-07-04 08:38, Sanchit wrote:
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().

--
Erik Wikström
Jul 4 '07 #2

P: n/a
AG
"Sanchit" <sa************@gmail.comwanted someone to do his home work
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.
Jul 4 '07 #3

P: n/a
On Jul 4, 10:53 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-07-04 08:38, Sanchit wrote:
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().
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 4 '07 #4

P: n/a
On Jul 4, 11:40 am, "AG" <a...@tb.frwrote:
"Sanchit" <sanchitgupt...@gmail.comwanted someone to do
his home work 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())
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 4 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.