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(cf gfile, 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;
}
---- 6 4963
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(cf gfile, 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
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(cf gfile, 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.
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=Value1 0 Key2=Value1 5 Key3=Value2 0 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.ope n ("/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(cf gfile, 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::norepla ce 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.
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
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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
-----------------------------------------------
#include <iostream.h>
#include <fstream.h>
void main()
|
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.
I couldn't figure out which members could be used
to check for these types of errors.
I tried the good(), bad(), fail() etc.,
after writing to...
|
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 non-printing)...in Windows
you need the (0x0d 0x0a) pair, if you want a new-line in a text
editor. What's the correct way to get this 2-byte sequence into a
file?...
|
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++ functions that do
this or is there any way I can check manually?
Thanks,
Prashant
|
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 suggestions how I can make my code
better. If you need more code I will post it. I am in early
devlopment of the game.
map.h:
| |
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 ofstream constructor (<fstream.h>) accepts a third parameter (int
nProt = filebuf::openprot)
i.e. ofstream( const char* szName, int nMode = ios::out,...
|
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 other things.
What would be a good design to accomplish these goals? Here are the
ideas that I have come up with:
Class X = class with data that I...
|
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 and holds
everything a unit in my game is supposed to do. What are some some
cures for this kind of large object or are they OK because they...
|
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 one function that is suppose to count the words in the file but it isnt working
I have a test file with two lines in it.
abc def ghi gk
ades
...
|
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...
|
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...
| |
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. ...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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...
|
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...
|
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...
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |