fscanf whitespace

This is a discussion on fscanf whitespace within the C Programming forums, part of the General Programming Boards category; hi guys, im currently having problems looking for invalid whitespace at the front of a string when using fscanf to ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    fscanf whitespace

    hi guys,

    im currently having problems looking for invalid whitespace at the front of a string when using fscanf to read the input.

    For example:
    Code:
    void check()
    {
        char letter;
        int number;
        char input[3];
       
        printf("Please enter letter and number :");
        fscanf(stdin, "%s", stdin);
        letter = input[0];
        number = input[1];
    
       if(letter == ' ' || letter == NULL)
       {
           check();
       }
    }
    but if im entering

    Please enter letter and number :*space*L7 or more than one space it still allows as valid...... if anyone can help me with this problem would be much appreciated

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by BlkR View Post
    Please enter letter and number :*space*L7 or more than one space it still allows as valid...... if anyone can help me with this problem would be much appreciated
    fscanf() ignores whitespace by design. You are basically asking how to use a function for something it was specifically designed NOT to do. The answer is that fscanf() is a bad choice.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    ..

    so should i be using scanf or fgets ?

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You should be using fgets().

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    ...

    basically i want to actually check for whitespaces in front of the string... so using fgets i would be able to implement it?

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by BlkR View Post
    basically i want to actually check for whitespaces in front of the string... so using fgets i would be able to implement it?
    May I ask, why do you care if the user enters whitespace? Is it really such a bad thing?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char buf[ BUFSIZ ] = {0};
    char *ptr = buf;
    
    fgets( buf, BUFSIZ, stdin );
    while( isspace( *ptr ) )
        ptr++;
    I don't know if you can implement it, but it can be done fairly easy. You could always skip what I've shown, and just follow the fgets call up with sscanf to parse it, and not care if it starts with whitespace or not.


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

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    ...

    cos in my exercise that im doing, it has to do some error check as it wont allow anymore than 2 variables....

    eg:

    l2 <===fine
    h3 <=== fine
    *space*h2 <==== not fine
    h21544 <===not fine
    hsdgf2345 <=== not fine

    sorry for the confusion

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by BlkR View Post
    cos in my exercise that im doing, it has to do some error check as it wont allow anymore than 2 variables....
    How is whitespace a "variable?" I guess if that's what the assignment says to do, you have to do it. But it's dumb.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    ...

    i mean it would only accept the sample above but wont accept anything else

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    How about just:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      int letter, number, nl;
    
      printf("Please enter letter and number: ");
      fflush(stdout);
      letter = getchar();
      number = getchar();
      nl = getchar();
    
      if(!isalpha(letter) || !isdigit(number) || nl != '\n')
        puts("You screwed up");
      else
        puts("You win!");
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. white space and fscanf
    By DMaxJ in forum C Programming
    Replies: 2
    Last Post: 06-10-2003, 09:18 AM
  4. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM
  5. ignoring whitespace with fscanf
    By Viny in forum C Programming
    Replies: 1
    Last Post: 11-22-2001, 03:05 PM

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