Like Tree3Likes

Problem with scanf and fscanf

This is a discussion on Problem with scanf and fscanf within the C Programming forums, part of the General Programming Boards category; I have a problem when trying to read this kind of format (someNumber) from file or standard input, so there ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    Problem with scanf and fscanf

    I have a problem when trying to read this kind of format (someNumber) from file or standard input, so there is some problem with this conversion string "(%d)" in scanf and fscanf.How to solve that and why these programs don't work well?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main () {
         int i=4;
         while (i!=3) {
         scanf("(%d)",&i);
         printf("test");
         }
         system("pause");
         
         }
    and

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main () {
    int i;
    FILE *fp;
    fp = fopen("dat.txt","w");
    for (i=1;i<=10;i++)fprintf(fp,"(%d)\n",i);
    fclose(fp);
    
    fp = fopen("dat.txt","r");
    while(!feof(fp)){
    fscanf(fp,"(%d)",&i);
    printf("\n%d",i);
    }
    
    fclose(fp);
    system("pause");
    }
    Last edited by Dusko; 06-09-2011 at 08:25 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,589
    Code:
    scanf(" (%d)",&i);
    Did you try it with a space before the "("?
    It works for me. MinGW GCC

    Tim S.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Nope,it doesn't work but i also need some explanation.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    Also, your scanf leaves the newline character in the input buffer, so the next time around it tries to read it and fails. For example, do the right thing and check the return value of scanf (it returns the number of matching inputs) and see what happens (note I also fixed your code to have the proper definition of main, as well as proper indentation and not using Windows-specific bad practice such as system("pause")):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void) {
        int i;
        FILE *fp;
        fp = fopen("dat.txt","w");
        for (i=1;i<=10;i++)fprintf(fp,"(%d)\n",i);
        fclose(fp);
    
        fp = fopen("dat.txt","r");
        while(!feof(fp)){
            if (fscanf(fp,"(%d)",&i) == 1)
                printf("%d\n",i);
        }
    
        fclose(fp);
        getchar();
    }
    So you need to account for that newline in your scanf format. One way to do it:
    Code:
    if (fscanf(fp, "(%d)%*c", &i) == 1)
        printf("%d\n", i);
    which in the %*c says "scan in all the remaining characters in the buffer but ignore them".
    Dusko likes this.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Thank you on your reply it was helpful, I accomplished same thing with fscanf(fp, "(%d)", &i); fgetc(fp); but it prints 10(which is the last number) two times and i'm not sure why. Second thing why scanf leaves '\n' in buffer in first and in second example, as i know it doesnt leave something in buffer regulary ? How to solve problem in first example, i was trying with fflush but i couldn't get it wright.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    Scanf always leaves the trailing newline in the buffer, unless you specifically read it in. It's not a problem if scanf is followed by another scanf, but when it's followed by a function like getchar(), which doesn't skip whitespace, then it becomes something to be aware of.

    You really should loop on scanf, not feof:
    Code:
    while(fscanf(fp,"(%d)",&i) == 1)
                printf("%d\n",i);
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    This doesnt works it only prints 1, probably because '\n' stays in buffer and after when fscanf is called again it returns 0. You said that it's not problem if scanf is followed by another scanf but why doesnt first example work well, in that example function scanf is called more than several times?
    Code:
    while(fscanf(fp,"(%d)",&i) == 1)
                printf("%d\n",i);

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Dusko View Post
    This doesnt works it only prints 1, probably because '\n' stays in buffer and after when fscanf is called again it returns 0. You said that it's not problem if scanf is followed by another scanf but why doesnt first example work well, in that example function scanf is called more than several times?
    Code:
    while(fscanf(fp,"(%d)",&i) == 1)
                printf("%d\n",i);
    %d is more than happy to skip leading whitespace. But ( isn't -- that means the very next character must be a left parenthesis.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    OK but why in this example it skips second scanf
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main () {
         int i;
      
       scanf("%d",&i);
        scanf("(%d",&i);
       printf("%d",i);
        
         system("pause");
         
         }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That depends on what you're trying to read. The format, when you stick it all together, is basically "%d(%d", so you must have a number and then a parenthesis (with no space (and space also means "enter key") in between) and then another number. If you don't have that format in your input, then you've been lying to scanf, and when you lie to scanf you don't get any data.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Aren't 2 scanf functions suppose to be separate, so that first function read sth from the buffer after pressing enter and after that second func. should be called?
    Is it true that if sth stays in buffer after one scanf function then if you write this scanf("%c",&i); // i='\n' so like if sth stays buffer and you assign that to some char , that char equals to '\n'?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A buffer is just "a big pile of memory where things get stored". As you type things go into the buffer, and then you read them out. Enter-key just goes in the buffer with everything else.

    EDIT: I should be a bit clearer. There's the input buffer that your program sees -- if you are reading from a file, then the whole file is essentially your input buffer; if you are reading from the keyboard, then things get added to the input buffer when you press enter-key. But when people speak of buffered input, they often mean just that last little bit, that allows you to backspace and correct errors before committing the input to the input buffer.
    Last edited by tabstop; 06-09-2011 at 01:23 PM.

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    This is confusing.The main thing that i'm not sure will something stay in buffer after scanf. For example second scanf in this doesnt work
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main () {
        char i,b,c;
      
       scanf("%c.%c",&i,&b);
        scanf("%c",&c);
       printf("%c",c);
        
         system("pause");
         
         }
    but in this code it does work
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main () {
        char i,b,c;
      
       scanf("%c.%c",&i,&b);
        scanf("%d",&c);
       printf("%c",c);
        
         system("pause");
         
         }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Everything is always left in the buffer unless it is read. All those scanfs "work" in that they do exactly what they say they're going to do (read in an integer or a character respectively). Why do you think they don't?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rags_to_riches View Post
    which in the %*c says "scan in all the remaining characters in the buffer but ignore them".
    No it doesn't. It says to scan in one remaining character and don't store it.

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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  2. fscanf problem maybe?
    By zdream8 in forum C Programming
    Replies: 13
    Last Post: 05-24-2008, 03:33 PM
  3. problem I am having with fscanf()
    By trancekid in forum C Programming
    Replies: 2
    Last Post: 09-29-2007, 10:22 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM
  5. Problem with fscanf..
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 02:56 PM

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