Thread: Read and change value from config file

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    14

    Read and change value from config file

    Is there any other way to read and change value from some line in config file than to jump to specified line and parse whole string from it? I also need to do some modifications (change value) on that parsed line, and write it back to file.

  2. #2
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    Do you know the line number or do you have to find out what line number that the text string you want to edit is located on? It is easy to read a text file line by line.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    14
    Quote Originally Posted by FourAngels View Post
    Do you know the line number or do you have to find out what line number that the text string you want to edit is located on? It is easy to read a text file line by line.
    I know line number now, but it would be good if it will be dynamically changed.

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    You could try to open the file and read the line that you want, edit it by writing a new line, than overwrite the file at that line location with the edited line of text. It might work.

  5. #5
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    I tried it finally. This was the result. It overwrote the original line however the new line was not as long as the original, so it looks like it could be a problem.
    Code:
    Here is some text line 1
    Here is more text line 2
    A new line is here.xt line 3
    This is the the last line of text line 4
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        FILE *fptr = fopen("/pathname/news.txt", "r+");
        if (fptr == NULL) exit(1);
        char buffer[256];
        char newline[] = "A new line is here.";
    
        int line = 2;
    
        while( fgets(buffer, 256, fptr) != NULL) {
    
            line -=1;
    
            if (line == 0) {
                fflush(fptr);
                fputs(newline, fptr);
            }
    
        }
        return 0;
    }
    Code:
    
    

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    In my config files, I parse the whole file, store it in memory, and if I need to change something in the file, I truncate the file and write back the whole thing from the in-memory representation. Unless the config file is absolutely huge, this is the best way I've found to do it, but your needs may be different from mine.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 29
    Last Post: 10-21-2013, 06:06 PM
  2. Where to put a config file
    By KirkHammett in forum C# Programming
    Replies: 2
    Last Post: 01-24-2010, 11:53 PM
  3. config file interface
    By valis in forum C Programming
    Replies: 3
    Last Post: 08-17-2006, 09:30 PM
  4. Replies: 12
    Last Post: 03-10-2005, 07:48 PM
  5. knoppix config file
    By sentienttoaster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-17-2004, 09:23 PM

Tags for this Thread