problem reading string from file

This is a discussion on problem reading string from file within the C Programming forums, part of the General Programming Boards category; Helo forums, I'me trying to read config file, - here's my c code: Code: void GetConf(){ FILE* config_fp; char line[MAX_LINE_LEN+1]; ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    problem reading string from file

    Helo forums,
    I'me trying to read config file, - here's my c code:
    Code:
    void GetConf(){
    FILE* config_fp;
    char line[MAX_LINE_LEN+1];
    char* var_word;
    printf("\nReading config...\n");
    config_fp = fopen("them.conf","r");
    while(fgets(line, MAX_LINE_LEN, config_fp) != NULL){
        var_word=strtok(line, "\t =\n\r");
        if(var_word != NULL && var_word[0]!='#'){
            if(!strcmp(var_word,"hi_temp")){
                var_word=strtok(NULL, "\t =\n\r");
                hi_temp=atoi(var_word);
            }
            if (!strcmp(var_word,"lo_temp")){
                var_word=strtok(NULL, "\t =\n\r");
                lo_temp=atoi(var_word);
            }
            if (!strcmp(var_word,"log_file")){
              var_word=strtok(NULL, "\t =\n\r");
                log_file=var_word;
                printf ("Value inside clause: %s\n",var_word);
            }
        }
    }
    printf ("Value outside clause: %s",log_file);
    printf ("Done.\n");
    }
    Config file:

    log_file=log
    hi_temp=25
    lo_temp=10
    correction=20

    Otput:

    Reading config...
    Value inside clause: log
    Value outside clause: 600Done.
    ###################################

    How does this happen? - why 'log_file' value suddenly becomes 600 ?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > log_file=var_word;
    1. var_word is a local variable, so even if it appeared to work, it would not survive the return from the function.
    2. You only copy the pointer, not what it's pointing at. So any change to the data affects both pointers.

    You need
    char log_file[MAX_LINE_LENGTH];

    Then
    strcpy(log_file,var_word);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    Thank you- this works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Replies: 22
    Last Post: 12-23-2008, 12:53 PM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Problem reading from a file..
    By Candelier in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 12:42 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

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