Thread: bad fcanf() format

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    bad fcanf() format

    Hello.

    this is code of piece of my program:

    Code:
      FILE *fp;
      if((fp = fopen("prog_conf.txt", "r")) == NULL)
      {
        printf ("\nNie mogę otworzyć pliku prog_conf.txt do zapisu!");
        return NULL;
      }
      printf ("\nPlik otwarto!\n");
    
      while( fscanf(fp,"%s#%s",optionName,optionValue) != EOF)
      {
        printf("\n%s : %s",optionName,optionValue);
      }
    
      fclose(fp);

    and here is structure of prog_conf.txt file:

    Code:
    log_file#log.txt
    mysql_server#localhost
    mysql_user#root
    mysql_pass#asd
    mysql_db#schoolSubjects
    defaultnumquest#5
    If I use format as %s#%s then whole first part of file line is writing to variable optionName. I don't know what is wrong. I want to write first part of line(before #) to variable optionName and secound part of line (after #) to variable optionValue. Can you help me with that?:/

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The problem is %s will include #. Try this one:
    Code:
    "%[^#]#%s"
    [] indicates a set. Usually they are inclusive, eg [0-9,a-z] will capture any combination of digits and non-caps. ^ indicates exclusion, so [^#] will capture anything, not include #.

    I believe since you are using fscanf directly on the file you may end up with newlines getting left in the buffer (they are not eaten by %s, so %[^#] may pick them next. If that happens use %*c at the end of the template (* discards).
    Code:
    "%[^#]#%s%*c"
    Last edited by MK27; 03-28-2010 at 07:12 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    %s stops reading on whitespace

    you want to stop reading on #

    Code:
    %[^#]#%s
    something like that should work

    of cause adding width modifier to prevent buffer overrun would be safe thing to do...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Code:
    %[^#]#%s
    Thank you! It is working. But now there is a one little problem. In variable optionName as the first char there is writing char of the new line \n. and when i print this variable, it is in the new line. Is there in c a function like a trim() from php, or something like that?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mirekgn View Post
    Is there in c a function like a trim() from php, or something like that?
    No, but you could write one quite easily. However, that is not necessary. If you had read more than just the very last post, you should have noticed:

    Quote Originally Posted by MK27 View Post
    I believe since you are using fscanf directly on the file you may end up with newlines getting left in the buffer (they are not eaten by %s, so %[^#] may pick them next). If that happens use %*c at the end of the template (* discards).
    Code:
    "%[^#]#%s%*c"
    The '\n' you are getting at the beginning is left over from the end of the previous line, so if you discard it in the first place it won't be there later. %c is a single character (such as '\n', which is not included in %s) * indicates this item does not go into a variable but gets thrown away.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    yeah! Thank you so much.
    format:
    Code:
    %[^#]#%s%*c
    is working.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    And what now when I have line in file like that:
    Code:
    biologia#Nazwa nauczyciela#Tkanki i bakterie
    Format above is not working:/ I tried to change this format analogously to format above but I do it wrong. Can you write me correct format for writing 3 variables?

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Can someone help me with that?
    I think the problem is that %s stops reading on whitespace but I don't know how to do it in C language.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %[ ...scan set... ] = put between brackets what you want to scan for
    start it with ^ if you want to scan for stuff NOT in the brackets
    %*c = skip one character (the * says to not store what you're scanning for)


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about to format :/
    By MisterSako in forum Tech Board
    Replies: 7
    Last Post: 06-13-2005, 07:51 PM
  2. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 11:42 PM
  3. Can't Format C
    By caroundw5h in forum Tech Board
    Replies: 40
    Last Post: 04-26-2004, 09:57 AM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM