Invalid input in text file
Author |
Message |
Shannon S. Harv #1 / 12
|
 Invalid input in text file
I am writing a program which uses fscanf to read data from a file. This program is a stack of "cars" and I first read the tag #, followed by whether it's an arrival or departure (on arrival-it is stored in a stack called garage, on departure-it is found and stored in another stack called free_list). The problem I'm having is, the instructor said we need to error check extensively (including for "extra" data in the input file)...for example...a good data line might read... 1234a signifying car tag # 1234 is an arrival... however, what do you do if you have... 1234ac here is the statement I'm using... status = fscanf (inFilePtr, "%4d%c%c", &tag_num, &enter_exit, &cr); /* check status to confirm fscanf is successful */ if (status != 3 || cr != '\n') { fprintf (outFilePtr, "%d%c", tag_num, enter_exit); fprintf (outFilePtr, "Invalid input---no action taken.\n"); fprintf (outFilePtr, "Moving to next line of data...\n"); Quote: }
The problem here is, that when I return back to that fscanf statement in my loop on the next pass, fscanf DOESN'T skip to the next line of data in the input file or even to the extra character (in this case, the "c" in 1234ac). As a matter of fact, it doesn't appear to do anything at all! Any assistance is GREATLY appreciated... signed... frustrated --
|
Fri, 23 Feb 2001 03:00:00 GMT |
|
 |
Oliver Walte #2 / 12
|
 Invalid input in text file
Quote:
> I am writing a program which uses fscanf to read data from a file. This
...... Quote: > The problem here is, that when I return back to that fscanf statement in > my loop on the next pass, fscanf DOESN'T skip to the next line of data in > the input file or even to the extra character (in this case, the "c" in > 1234ac). As a matter of fact, it doesn't appear to do anything at all! > Any assistance is GREATLY appreciated...
(Sorry, had to remove part of the original post because my network won't post when there's more quoted text than new text). Lots of people have problems using fscanf. See the FAQ questions 12.17, 12.18, 12.20 The usual more controllable way is to get a line of data with fgets and then use sscanf, strtol, atoi etc. to decipher what's in it. -- Oliver Walter Marconi Electronic Systems, Dynamics Division Stanmore HA7 4LY, U.K.
--
|
Fri, 23 Feb 2001 03:00:00 GMT |
|
 |
Francis Glassboro #3 / 12
|
 Invalid input in text file
Quote: >The problem here is, that when I return back to that fscanf statement in >my loop on the next pass, fscanf DOESN'T skip to the next line of data in >the input file or even to the extra character (in this case, the "c" in >1234ac). As a matter of fact, it doesn't appear to do anything at all! >Any assistance is GREATLY appreciated...
My preference would be to use fgets() to read in an entire line, then use sscanf to pull out the relevant fields. You need to familiarise yourself with the whole library not just the most popular (and often overused) functions. Quote: -- Francis Glassborow --
|
Fri, 23 Feb 2001 03:00:00 GMT |
|
 |
David Harm #4 / 12
|
 Invalid input in text file
Quote:
>The problem here is, that when I return back to that fscanf statement in >my loop on the next pass, fscanf DOESN'T skip to the next line of data in >the input file or even to the extra character (in this case, the "c" in >1234ac). As a matter of fact, it doesn't appear to do anything at all!
In such a situation, I recommend: 1. Use fgets() to read the file line-by-line. 2. Use sscanf() to parse the lines after you read them. That way, when a parse error occurs you have the data in your hands to include in an error message, write to an exception file for manual cleanup and reprocessing, or whatever else you want. And you don't have to worry about re-synching with the file to continue. --
|
Sat, 24 Feb 2001 03:00:00 GMT |
|
 |
Erik Oostero #5 / 12
|
 Invalid input in text file
I am a student of C myself, but does flush help at al? ERIK Quote:
> I am writing a program which uses fscanf to read data from a file. This > program is a stack of "cars" and I first read the tag #, followed by > whether it's an arrival or departure (on arrival-it is stored in a stack > called garage, on departure-it is found and stored in another stack > called free_list). The problem I'm having is, the instructor said we > need to error check extensively (including for "extra" data in the input > file)...for example...a good data line might read... > 1234a > signifying car tag # 1234 is an arrival... > however, what do you do if you have... > 1234ac > here is the statement I'm using... > status = fscanf (inFilePtr, "%4d%c%c", &tag_num, &enter_exit, &cr); > /* check status to confirm fscanf is successful */ > if (status != 3 || cr != '\n') > { > fprintf (outFilePtr, "%d%c", tag_num, enter_exit); > fprintf (outFilePtr, "Invalid input---no action taken.\n"); > fprintf (outFilePtr, "Moving to next line of data...\n"); > } > The problem here is, that when I return back to that fscanf statement in > my loop on the next pass, fscanf DOESN'T skip to the next line of data in > the input file or even to the extra character (in this case, the "c" in > 1234ac). As a matter of fact, it doesn't appear to do anything at all! > Any assistance is GREATLY appreciated... > signed... > frustrated > --
--
|
Sat, 24 Feb 2001 03:00:00 GMT |
|
 |
WROT #6 / 12
|
 Invalid input in text file
Quote: >> 1234a >> signifying car tag # 1234 is an arrival... >> however, what do you do if you have... >> 1234ac >> here is the statement I'm using... >> status = fscanf (inFilePtr, "%4d%c%c", &tag_num, &enter_exit, &cr); >> /* check status to confirm fscanf is successful */ >> if (status != 3 || cr != '\n') >> { >> fprintf (outFilePtr, "%d%c", tag_num, enter_exit); >> fprintf (outFilePtr, "Invalid input---no action taken.\n"); >> fprintf (outFilePtr, "Moving to next line of data...\n"); >> } >> Any assistance is GREATLY appreciated...
Well, first I would like to point out one of those things they don't teach you in these classes, to wit, that the entire scanf() family of functions is a tool of Satan! Satan, I say! I never could get used to the odd newline handling (or lack thereof) in these functions. In a case like this I would probably get the whole line from the file using fgets(), then parse each field seperately. Be warned-the parsing part can get ugly if you do this, if the numeric field is variable in length. It should work though, and "ugly, but it works" is the kind of code C programming is all about ;). -W. Roth
--
|
Sat, 24 Feb 2001 03:00:00 GMT |
|
 |
Oliver Walte #7 / 12
|
 Invalid input in text file
Quote:
> I am a student of C myself, but does flush help at al? ERIK
Not during input, i.e. when reading a file; its effect is undefined. It might sometimes help during output (and it's called fflush). -- Oliver Walter Marconi Electronic Systems, Dynamics Division Stanmore HA7 4LY, U.K.
--
|
Sat, 24 Feb 2001 03:00:00 GMT |
|
 |
Francis Glassboro #8 / 12
|
 Invalid input in text file
writes Quote: >12.19 I'm re-prompting the user if scanf fails, but sometimes it seems to go >into an infinite loop.
Of course because it leaves the problem in the input buffer until you remove the relevant character. Quote: >12.26 Will fflush(stdin) flush unread characters from the standard input >stream?
No. Try while(getchar() != '\n'); Which should read up to an including the carriage return. Francis Glassborow Chair of Association of C & C++ Users 64 Southfield Rd Oxford OX4 1PA +44(0)1865 246490 All opinions are mine and do not represent those of any organisation --
|
Sun, 25 Feb 2001 03:00:00 GMT |
|
 |
Andreas Schwa #9 / 12
|
 Invalid input in text file
I LIKE it. I HATE it. I LIKE it. I HATE it. I LIKE it. I HATE it. I LIKE.. EMOTIONS are SWEEPING over me!! X-Newsreader: Gnus v5.6.42/Emacs 19.34
|> writes |> >12.19 I'm re-prompting the user if scanf fails, but sometimes it seems to go |> >into an infinite loop. |> Of course because it leaves the problem in the input buffer until you |> remove the relevant character. |> > |> >12.26 Will fflush(stdin) flush unread characters from the standard input |> >stream? |> |> No. Try |> while(getchar() != '\n'); |> Which should read up to an including the carriage return. And goes into an infinite loop if there isn't one before the next EOF or read error. -- Andreas Schwab "And now for something
--
|
Sun, 25 Feb 2001 03:00:00 GMT |
|
 |
Francis Glassboro #10 / 12
|
 Invalid input in text file
Quote: >And goes into an infinite loop if there isn't one before the next EOF or >read error.
Sorry I am so used to using getchar() in systems where there must be a CR because it is reading the keyboard that I tend to forget that redirection might mean your data was coming from somewhere else:) Francis Glassborow Chair of Association of C & C++ Users 64 Southfield Rd Oxford OX4 1PA +44(0)1865 246490 All opinions are mine and do not represent those of any organisation --
|
Mon, 26 Feb 2001 03:00:00 GMT |
|
|
|