473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2390
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.comwante d 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("myfil e.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,bu ff);
string::size_ty pe pos=buff.find(t ext1);
if(pos==string: :npos)
{
cout << "Error : could not find the string "
cout << text1 << " in file " << filename << "\n";
return 1;
}
buff.replace(bu ff.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 objektorientier ter Datenverarbeitu ng
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.frwrot e:
"Sanchit" <sanchitgupt... @gmail.comwante d 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("myfil e.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,bu ff);
And of course, you have to check that this succeeded if you
don't want to get into trouble later.
string::size_ty pe pos=buff.find(t ext1);
if(pos==string: :npos)
{
cout << "Error : could not find the string "
cout << text1 << " in file " << filename << "\n";
return 1;
}
buff.replace(bu ff.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 objektorientier ter Datenverarbeitu ng
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
4257
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 bottom of the page, where I can edit the fields, and save back to the same line in the text file. I dont know how to use primary keys or anything...
1
8353
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 manipulate the data. I have vb .net 2003 but when I open the .exe file it shows me the binary info and not the windows that was developed. The window...
0
3088
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. However, the DataGrid begins to get very cumbersome as the number of columns of data you present increases. Anything more than half a dozen columns...
4
3704
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 file via some editable controls such as text boxes , option boxes etc. how can i implment this , should i use another xslt file with <INPUT>...
0
1224
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 application and open the file in a new tab. I cant find the correct settings to do this is 'FILE TYPES', could anyone help? My current settings are...
1
1568
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 file), I get the error below. This is getting to be a nightmare, and I'd love some suggestions on how to stop it. I have the Edit and Continue setting...
1
2155
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 I have Categories and various Products in each Category. I'm trying to build a page to EDIT/UPDATE each product.I want to be able to change the...
1
2114
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 same, and I would like to find a way to make it easily editable. How do I edit the text file (a single line needs to be changed) using a Visual...
1
3599
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 extension. I guess this is a Flash Movie file type? My question is: What program do I use to edit this file? I have both Macromedia...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.