Thread: C linux - reading config file.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    C linux - reading config file.

    I am tring to figure out how to write something that will read/write to a config file on linux. I been searching around the forums/google. Even the ansi C book I have. I am confused. I tried reading on fopen, fprintf, fgets. <-- Things like that. But still can't figure out how to do it. .

    The standard config file type.
    [n1]
    on=1
    trigger=Sometext
    [n2]
    on=1
    trigger=Sometext

    Say for example I want to grab the text after trigger= then use it somewhere else in my app. I am not sure how that is done. To me it seems like something simple.

    I figured maby something like this could be used to start it, but after that. I am confused.

    Code:
    #include <stdio.h>
    
    int main() {
    char test[256];    
    FILE *file;  
    
      file = fopen("somefile", "r"); 
    
      if(file==NULL) {
        printf("Error: can't open file.\n");
        return 1;
      }
      else {
        while(fgets(test, 10, file)!=NULL) { 
             }
        fclose(file);
        return 0;
      }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The function you're probably looking for is sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I suspect there is a library for it, though, since this configuration "format" is very popular.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    1
    Maybe what you want is: iniparser
    iniParser: stand-alone ini Parser library in ANSI C

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Have you try googling parsing linux config file?
    Key-value file parser
    Linux configuration file libraries - Stack Overflow

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or bumping 2 year old threads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM