473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can i edit a file

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
4 2379
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say, a radio button) it will load into the form at the...
1
by: Tom | last post by:
Hello I am a newbie to VB .net 2003 environment. I have a database in sql server 2000 that was developed using vb. I would like to edit some of the .exe files that are used to collect and...
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
0
by: colin.steadman | last post by:
If I right click on a ASP file in Explorer and choose EDIT, Windows opens a new instance of MSE.EXE and opens the file in that. What I'd like it to do is simply goto the existing open MSE...
1
by: KJ | last post by:
I am building an ASP.NET 2.0 Web Site in VS 2005 Team Edition SP1. Every time I edit any HTML (not just server controls, even if I edit a div), or even if I edit the stylesheet (yes, the .css...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
1
by: Xicon | last post by:
I am looking to create a program that is able to edit a text file that is not located within the program. This particular text file is always in the exact same location and is always named the exact...
1
by: Primo | last post by:
I'm trying to figure out how to make some small text changes to my website, but don't have any experience doing it. I've managed to figure out that the file which needs to be changed has an SWF...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.