File editing problem.

This is a discussion on File editing problem. within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm trying to write simple configure file manager class. Configure values are stored in a map<string, string> configDictionary (first ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    File editing problem.

    Hello.
    I'm trying to write simple configure file manager class.
    Configure values are stored in a map<string, string> configDictionary
    (first string is property, second value).

    When some value in that map is changed, I want to write that new value in a configure file.
    For that, I wrote a method, which is listed bellow:

    Code:
    typedef map<string, string>::iterator confIterator_d; // iterator
    map<string, string> configDictionary;
    string configFileName; //name of the config file
    string lastError; //error msg
    
    int ConfigManager::SaveConfigFile(const string &key)
    {
        fstream config;
        config.open(configFileName.c_str(), ios::in | ios::out);
        if (!config.is_open())
        {
            lastError = "Can't read file.";
            return 1;
        }
    
        string tmp;
        unsigned pos = 0;
        while (getline(config, tmp))
        {
            size_t found = tmp.find(key);
            if (found != string::npos)
            {
                confIterator_d it = configDictionary.find(key);
                if (it != configDictionary.end())
                {
                    tmp = it -> first + "=" + it -> second;
                    config.seekp(pos, ios::beg);
                    config.write(tmp.c_str(), tmp.length());
                    break;
                }
            }
            pos += tmp.length();
        }
        config.close();
        return 0;
    }
    key is a name of property which I want to save.
    I'm reading file line by line. In each line
    I'm looking for the key. If key is found, create new line (tmp)
    and write it in the proper line of the file.

    Question is, why it doesn't work? I just can't save the new value.
    Last edited by opat; 09-09-2011 at 05:58 AM. Reason: Bad title

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,683
    Question is, why it doesn't work?
    What exactly is not working?

    Are you trying to replace one string with another string?

    Jim

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have a text file, you are unlikely to be able to replace on top of your old file, unless the new bit is exactly the same as the old bit.

    You are also always seeking to 0 (the beginning of the file). Are you trying to append, or overwrite?

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Yes, that is exactly what I want to do.
    I used test file
    Code:
    test1 = val1
    cha1 = val2
    cha2 = val3
    and I wanted to change property cha2 from val3 to val4.
    I used my method but the file did not changed.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Unless you are absolutely certain that the new value will always and forever be the same size as the old, you can't just write to the file (because you will then either leave bits of the old line behind, if the new one is shorter, or crunch the next line if the new one is longer). You must write a new file instead.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Quote Originally Posted by tabstop View Post
    If you have a text file, you are unlikely to be able to replace on top of your old file, unless the new bit is exactly the same as the old bit.

    You are also always seeking to 0 (the beginning of the file). Are you trying to append, or overwrite?
    If I use binary file will I be able to replace parts of my file with new strings?
    I'm trying to overwrite my file with new config value. Rest values have to stay intact.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by opat View Post
    If I use binary file will I be able to replace parts of my file with new strings?
    Nope.
    (You won't have to write a different file if you read the entire file into memory, change the contents in memory, then write out the entire contents back to the file. You will still have to process the entire file however.)

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by opat View Post
    Question is, why it doesn't work? I just can't save the new value.
    The problem is that text files use variable length lines, making them unsuitable for seek operations... where do you seek to?
    If the new line is shorter than the old one you leave parts of the old line behind in the file.
    If the new line is longer you end up overwriting the beginning of the next line.

    With text files your only viable choice is to re-write the entire file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Editing an include.h file
    By Char*Pntr in forum C Programming
    Replies: 5
    Last Post: 08-16-2010, 11:31 AM
  2. Editing a text file
    By bijan311 in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2010, 11:04 AM
  3. editing large file
    By arcamot in forum Windows Programming
    Replies: 7
    Last Post: 09-07-2007, 06:48 PM
  4. Editing a text file
    By rehan in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2007, 09:58 AM
  5. editing file
    By hmunhung in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2002, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21