Thread: scanf question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    scanf question

    How can I make scanf skip a blank line....

    I want it to fscanf some numbers

    input file:
    Code:
    123
    12235
    
    343
    643
    I'm using a while loop
    but the blank space is throwing everything off

    how can i tell scanf to skip that empty space and read the number in the next line?

    This is my code, although I dont know if it
    makes sense cause I'm trying to do other stuff to:
    Code:
    while( strstr("class siz",temp_name) == NULL) {
    
    
        
        fgets(temp_name,30,fin);
    
        if(strlen(temp_name)>9){
            
               temp_name[9] = '\0' ; }
                            
    
    fscanf(fin," %d",&ID);                               
    
    
    
    
    
    printf("%d\n",ID);
    Thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How can I make scanf skip a blank line....
    You don't. scanf removes leading whitespace for the %d format modifier by default. I think what you're asking is how do you test to see if a string represents an empty line, since you use fgets to read the line and fscanf to parse it. The answer is that you only need to test the first character to see if it's a newline:
    Code:
    while ( fgets ( buffer, sizeof buffer, in ) != NULL ) {
      if ( buffer[0] != '\n' )
        fputs ( buffer, out );
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 08:48 AM
  4. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 10:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM