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

Discussion in 'C++' started by Ramesh, Oct 17, 2008.

  1. Ramesh

    Ramesh Guest

    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;

    }

    ----
    Ramesh, Oct 17, 2008
    #1
    1. Advertising

  2. Ramesh

    Barry Guest

    Re: Question of ofstream / fstream methods to modify a specific linein a text file.

    On Oct 17, 9:44 am, Ramesh <> 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
    Barry, Oct 17, 2008
    #2
    1. Advertising

  3. Ramesh

    Ramesh Guest

    Re: Question of ofstream / fstream methods to modify a specific linein a text file.

    On Oct 16, 6:57 pm, Barry <> wrote:
    > On Oct 17, 9:44 am, Ramesh <> 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::eek: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.
    Ramesh, Oct 17, 2008
    #3
  4. Ramesh

    red floyd Guest

    Re: Question of ofstream / fstream methods to modify a specific linein a text file.

    Ramesh wrote:
    > On Oct 16, 6:57 pm, Barry <> wrote:
    >> On Oct 17, 9:44 am, Ramesh <> 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::eek: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 :)


    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.
    red floyd, Oct 17, 2008
    #4
  5. Ramesh

    Guest

    Re: Question of ofstream / fstream methods to modify a specific linein a text file.

    On Oct 17, 4:09 am, Ramesh <> wrote:
    >
    > Yeah just learnt about replace after the compiler didnt like it - am
    > using ios::eek: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 :)
    >


    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, 2008
    #5
  6. Ramesh

    Ramesh Guest

    Re: Question of ofstream / fstream methods to modify a specific linein a text file.

    On Oct 17, 1:28 am, wrote:
    > On Oct 17, 4:09 am, Ramesh <> wrote:
    >
    >
    >
    > > Yeah just learnt about replace after the compiler didnt like it - am
    > > using ios::eek: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 :)

    >
    > 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
    Ramesh, Oct 17, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Armando
    Replies:
    6
    Views:
    736
    Armando
    Jan 29, 2004
  2. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    587
    Ivan Johansen
    Jul 14, 2005
  3. bballmitch

    fstream and ofstream

    bballmitch, Sep 1, 2005, in forum: C++
    Replies:
    2
    Views:
    464
    John Harrison
    Sep 1, 2005
  4. chiku

    fstream vs ofstream

    chiku, May 4, 2009, in forum: C++
    Replies:
    6
    Views:
    2,373
    Paul Carter
    May 5, 2009
  5. Tojuso
    Replies:
    3
    Views:
    84
    Tojuso
    Nov 27, 2007
Loading...

Share This Page