Thread: Discard wrong data from stream

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    Discard wrong data from stream

    I'm not understanding how to discard unwanted data in a file stream. For example I want to read in a list of doubles and only doubles from a file, if a non double is in the stream, some text for example, I need to flush it out of the stream and get the next item from the stream. The following code is in my textbook.
    Code:
    #define FLUSH while (fgetc(sp) !='\n')
    I just don't understand how to use it.

    Note: sp is the pointer to the file stream.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't forget about EOF.

    I wouldn't use a macro at all here, see the FAQ - http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Perhaps a function or just inline code.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    That code works, I guess that my problem is that while the buffer gets flushed it stops reading from the buffer. I need to keep reading from the buffer until EOF

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, what's wrong with what you posted -- it discards input until a new-line is reached.

    Is this interactive? If so, you won't go wrong not worrying about EOF; if it's not, you may want to check (files don't always end with a \n character).

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    I guess that I'm not sure where to call the macro: Here is the relevant code:
    Code:
       printf("Enter File Name:  ");
        scanf("%s",fileName);
        printf("\n\n");
        
        sp = fopen(fileName,"r");
       
        if(!sp){ /* there is no file with given filename*/
            printf("Error could not open file %s for read. \n\n",fileName);
            exit(0);
        }else if( fgetc(sp)==EOF){ /* no data in file,fileName valid */
            printf("There were 0 files in %s.\n",fileName);
            exit(0);
        }
        
        /*************
         the fscanf function reads data in from the stream
         it takes 3 parameters: stream pointer,conversion code, where to place data
         *************/
     while (( getchar()) != '\n' & sp != EOF);
        while(k=(fscanf(sp,"%lf",&data[i++]) == 1)) {
            b += k++ ;
            
        }
     
        fclose(sp);

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably because there's nowhere for you to call in that code, since it assumes that there is no such thing as bad input.

    You can't try to read directly into data[i++], since i will change even if nothing got read in. Read into a temp variable (in a while loop that checks for EOF, not ==1); if good, assign temp to data[i++]; if not, FLUSH.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Wow, thx for the help.I give that a shot

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yet another example of how fscanf leads you down the garden path.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, sp ) != NULL ) {
        // do something with buff
        // maybe replace the fscanf call with a sscanf call
    }
    If the file is empty, then the body of the loop doesn't happen. So there is no need for something like if( fgetc(sp)==EOF)
    Which, if there is actually data in the file consumes a character, and just puts you out of sync for the first true read of the file with fscanf.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. pipe stream data I/O
    By vector7 in forum Linux Programming
    Replies: 1
    Last Post: 03-11-2003, 06:46 PM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM