On Oct 16, 6:57*pm, Barry <dhb2...@gmail.com> wrote:
> On Oct 17, 9:44*am, Ramesh <rrame...@gmail.com> wrote:
>
> > 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:

ut | 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.