473,394 Members | 1,735 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.

Question of ofstream / fstream methods to modify a specific line in atext file.

Hello,

I am using the ofstream class to create a text file with keys and
values like:

Key1=Value10
Key2=Value15
Key3=Value20

In case I need to set a new value for Key2, say value50 - I am able to
read and get the value, not sure how to replace that specific value
after '=' for a specific line - Please advice which class / method can
help me achieve this?

thanks
/R

Here is my code snippet:

-----
using namespace std;

#include <iostream>

bool SetVal4Key(std::string Key, std::string Value) {

ofstream cfgfile;
bool status = FALSE;
string sLine;
string buf;
UINT32 pos = 0;
string Delim = "=";
cfgfile.open ("/etc/config.txt", ios::noreplace | ios::app);
if (!cfgfile) {
cout << Failed to open config file - unable to continue" << endl;
return status;
}

while (!cfgfile.eof()) {

std::getline(cfgfile, buf);

// Dump the content for debugging purpose

len = buf.size();
pos = buf.find(Key, 0);

if (!pos) {
pos = buf.find(Delim, 0);

//Modify the string
buf.erase
buf= Key;
buf.append = Value;

// Write to the specific line in the file where the key is already
present
status = TRUE;
cfgfile.close();
status = TRUE;
}
cout << "Failed to locate the key" << endl;
}
return status;

}

----
Oct 17 '08 #1
6 4947
On Oct 17, 9:44*am, Ramesh <rrame...@gmail.comwrote:
Hello,

I am using the ofstream class to create a text file with keys and
values like:

Key1=Value10
Key2=Value15
Key3=Value20

In case I need to set a new value for Key2, say value50 - I am able to
read and get the value, not sure how to replace that specific value
after '=' for a specific line - Please advice which class / method can
help me achieve this?

thanks
/R

Here is my code snippet:

-----
using namespace std;

#include <iostream>

bool SetVal4Key(std::string Key, std::string Value) {

ofstream * * * *cfgfile;
bool * * * * * *status = FALSE;
string * * * * *sLine;
string * * * * *buf;
UINT32 * * * * *pos = 0;
string * * * * *Delim = "=";

cfgfile.open ("/etc/config.txt", ios::noreplace | ios::app);
if (!cfgfile) {
* * * * cout << Failed to open config file - unable to continue" << endl;
* * * * return status;

}

while (!cfgfile.eof()) {

* * * * std::getline(cfgfile, buf);

* * * * // Dump the content for debugging purpose

* * * * len = buf.size();
* * * * pos = buf.find(Key, 0);

* * * * if (!pos) {
* * * * * * * * pos = buf.find(Delim, 0);

* * * * * * * * //Modify the string
* * * * * * * * buf.erase
* * * * * * * * buf= Key;
* * * * * * * * buf.append = Value;

* * * * * * * * // Write to the specific line in the filewhere the key is already
present
* * * * * * * * status = TRUE;
* * * * * * * * cfgfile.close();
* * * * * * * * status = TRUE;
* * * * }
* * * * cout << "Failed to locate the key" << endl;}

return status;

}
ios::noreplace is none-standard.

There's no way to modify the file inplace with standard C++.
You can load the file into vector<string>
modifies the strings. then overwrite the original file.

if the file to too large to do so, find out the platform APIs to
modify inplace.

--
Best Regards
Barry
Oct 17 '08 #2
On Oct 16, 6:57*pm, Barry <dhb2...@gmail.comwrote:
On Oct 17, 9:44*am, Ramesh <rrame...@gmail.comwrote:
Hello,
I am using the ofstream class to create a text file with keys and
values like:
Key1=Value10
Key2=Value15
Key3=Value20
In case I need to set a new value for Key2, say value50 - I am able to
read and get the value, not sure how to replace that specific value
after '=' for a specific line - Please advice which class / method can
help me achieve this?
thanks
/R
Here is my code snippet:
-----
using namespace std;
#include <iostream>
bool SetVal4Key(std::string Key, std::string Value) {
ofstream * * * *cfgfile;
bool * * * * * *status = FALSE;
string * * * * *sLine;
string * * * * *buf;
UINT32 * * * * *pos = 0;
string * * * * *Delim = "=";
cfgfile.open ("/etc/config.txt", ios::noreplace | ios::app);
if (!cfgfile) {
* * * * cout << Failed to open config file - unable to continue" << endl;
* * * * return status;
}
while (!cfgfile.eof()) {
* * * * std::getline(cfgfile, buf);
* * * * // Dump the content for debugging purpose
* * * * len = buf.size();
* * * * pos = buf.find(Key, 0);
* * * * if (!pos) {
* * * * * * * * pos = buf.find(Delim, 0);
* * * * * * * * //Modify the string
* * * * * * * * buf.erase
* * * * * * * * buf= Key;
* * * * * * * * buf.append = Value;
* * * * * * * * // Write to the specific line in the file where the key is already
present
* * * * * * * * status = TRUE;
* * * * * * * * cfgfile.close();
* * * * * * * * status = TRUE;
* * * * }
* * * * cout << "Failed to locate the key" << endl;}
return status;
}

ios::noreplace is none-standard.

There's no way to modify the file inplace with standard C++.
You can load the file into vector<string>
modifies the strings. then overwrite the original file.

if the file to too large to do so, find out the platform APIs to
modify inplace.

--
Best Regards
Barry
Yeah just learnt about replace after the compiler didnt like it - am
using ios::out | ios::app in its place.
I am still digging to see if fseek / fsetpos related functions in
cstdio can be handy, but no clear idea yet :)

But thanks a bunch for your quick response.
Oct 17 '08 #3
Ramesh wrote:
On Oct 16, 6:57 pm, Barry <dhb2...@gmail.comwrote:
>On Oct 17, 9:44 am, Ramesh <rrame...@gmail.comwrote:
>>Hello,
I am using the ofstream class to create a text file with keys and
values like:
Key1=Value10
Key2=Value15
Key3=Value20
In case I need to set a new value for Key2, say value50 - I am able to
read and get the value, not sure how to replace that specific value
after '=' for a specific line - Please advice which class / method can
help me achieve this?
thanks
/R
Here is my code snippet:
-----
using namespace std;
#include <iostream>
bool SetVal4Key(std::string Key, std::string Value) {
ofstream cfgfile;
bool status = FALSE;
string sLine;
string buf;
UINT32 pos = 0;
string Delim = "=";
cfgfile.open ("/etc/config.txt", ios::noreplace | ios::app);
if (!cfgfile) {
cout << Failed to open config file - unable to continue" << endl;
return status;
}
while (!cfgfile.eof()) {
std::getline(cfgfile, buf);
// Dump the content for debugging purpose
len = buf.size();
pos = buf.find(Key, 0);
if (!pos) {
pos = buf.find(Delim, 0);
//Modify the string
buf.erase
buf= Key;
buf.append = Value;
// Write to the specific line in the file where the key is already
present
status = TRUE;
cfgfile.close();
status = TRUE;
}
cout << "Failed to locate the key" << endl;}
return status;
}
ios::noreplace is none-standard.

There's no way to modify the file inplace with standard C++.
You can load the file into vector<string>
modifies the strings. then overwrite the original file.

if the file to too large to do so, find out the platform APIs to
modify inplace.

--
Best Regards
Barry

Yeah just learnt about replace after the compiler didnt like it - am
using ios::out | ios::app in its place.
I am still digging to see if fseek / fsetpos related functions in
cstdio can be handy, but no clear idea yet :)
The problem is that if you tweak the file inplace, and your replacement
text is bigger, you will clobber text:

e.g.
Key=Value15
Key=Value20

If you replace "Value15" with "Value100", you will get

Key=Value15Key=Value100

It's much safer to read it all, modify it, and write it back out.

Oct 17 '08 #4
On Oct 17, 4:09*am, Ramesh <rrame...@gmail.comwrote:
>
Yeah just learnt about replace after the compiler didnt like it - am
using ios::out | ios::app in its place.
I am still digging to see if fseek / fsetpos related functions in
cstdio can be handy, but no clear idea yet :)
I'm afraid that fseek & fsetpos won't be much help as you're dealing
with ASCII files. I'd go with Barry's advice to read the whole file
and replace the specific line which needs to be modified before
writing the contents back to another file.

Cheers
Chris

Oct 17 '08 #5
On Oct 17, 1:28*am, news.chris.th...@gmail.com wrote:
On Oct 17, 4:09*am, Ramesh <rrame...@gmail.comwrote:
Yeah just learnt about replace after the compiler didnt like it - am
using ios::out | ios::app in its place.
I am still digging to see if fseek / fsetpos related functions in
cstdio can be handy, but no clear idea yet :)

I'm afraid that fseek & fsetpos won't be much help as you're dealing
with ASCII files. I'd go with Barry's advice to read the whole file
and replace the specific line which needs to be modified before
writing the contents back to another file.

Cheers
Chris
Thanks, yes I gave up - really not worth spending time :)
right now doing a removal of the old file and modified contents go
into a new file.

Regards
Ramesh
Oct 17 '08 #6
On 2008-10-17 18:45:18 -0400, Ramesh <rr******@gmail.comsaid:
On Oct 17, 1:28Â*am, news.chris.th...@gmail.com wrote:
>On Oct 17, 4:09Â*am, Ramesh <rrame...@gmail.comwrote:
>>Yeah just learnt about replace after the compiler didnt like it - am
using ios::out | ios::app in its place.
I am still digging to see if fseek / fsetpos related functions in
cstdio can be handy, but no clear idea yet :)

I'm afraid that fseek & fsetpos won't be much help as you're dealing
with ASCII files. I'd go with Barry's advice to read the whole file
and replace the specific line which needs to be modified before
writing the contents back to another file.

Cheers
Chris

Thanks, yes I gave up - really not worth spending time :)
right now doing a removal of the old file and modified contents go
into a new file.
Right. The key notion here is that file streams are streams: a sequence
of bytes, one after another. The usual approach to modifying a file is,
indeed, copying up to the point where you need to change, writing out
the changed text, and copying the remainder. Then, after the new file
has been written and closed, deleting the old one and renaming the new
one.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 18 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tom Johnson | last post by:
Hi all, I cant believe that Im stuck on such simple code, but I am so... Im trying to open a text file for writing but can never get the file to initially open. Heres what Im having trouble with....
2
by: steve | last post by:
Is there a way to catch file errors while writing to a file using ofstream? For ex., if the file is deleted or permissions changed by another process after it is opened, or when the disk is full....
13
by: J. Campbell | last post by:
I'm wanting to output a text header file in front of the data portion of an output file. However when I use "\n" or "endl" as end of line, it just puts a 0x0a in the file(which is...
3
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++...
11
by: enki | last post by:
I am writing a game and I am having trouble with moving the character on the map. Here is what I have right now. It involves win32 programming but that not my problem. I would like some...
1
by: Avner | last post by:
I have a C++ code which I want to upgrade to the standard C++ The code constructs a file and sets the file permission The new iostream library does not include filebuf::openprot The old...
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
1
by: gdarian216 | last post by:
okay I had a code that asked user to input a file name and instead i changed it to take the input from the command line. I now want to output results of some functions to an output file. I have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.