Ini File Reading using fscanf()

This is a discussion on Ini File Reading using fscanf() within the C Programming forums, part of the General Programming Boards category; I'm having a problem parsing an ini file. I know there are lots of post on this topic, and I ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    Ini File Reading using fscanf()

    I'm having a problem parsing an ini file. I know there are lots of post on this topic, and I have read many of them. My ini file have no sections, just one entry:

    font=tahoma.ttf

    My Code looks like this

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    static FILE *ini_file;
    char *font;
    
    void LoadConfig(){
    char Setting[20],Value[20];
    int EndOfFile = 0;
         if ((ini_file = fopen("config.ini", "r"))){
            EndOfFile = fscanf(ini_file, "%[^=]=%s", Setting, &Value);
            while (EndOfFile != EOF){
                  if(strcmp(Setting,"font") == 0){
                             strcpy(font,Value);
                             }
                  EndOfFile = fscanf(ini_file, "%[^=]=%s", Setting, &Value);
            }
            fclose(ini_file);
         }
    }
    The problem is that the value is never read into the font variable, it never gets to this line
    Code:
    strcpy(font,Value);

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    I'm sorry about this thread. After recompiling all my files the program worked!!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    It might work, but it's still broken.

    > strcpy(font,Value);
    font doesn't point anywhere, so you just trash some random memory locations.
    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.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    I discovered that and allocated some memory for it. Thanks for your reply anyway!
    Last edited by Mr.Modem; 08-13-2005 at 09:37 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Your loop should be this
    Code:
        while ( fscanf(ini_file, "%[^=]=%s", Setting, Value) == 2 ) {
            if ( strcmp(Setting,"font") == 0 ) {
                strcpy(font,Value);
             }
        }
    Though it is better to read the file using fgets(), then scan the buffer using sscanf, then at least you get control over potential buffer overflow problems.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Using fscanf for reading numerical data from text file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-15-2002, 05:18 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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