Invalid input in text file
Author |
Message |
Shannon S. Harv #1 / 9
|
 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 --
|
Thu, 01 Mar 2001 03:00:00 GMT |
|
 |
gartner e #2 / 9
|
 Invalid input in text file
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 > --
use fgets followed by a sscanf end_of_file = fgets (buf,sizeof(buf),infilePtr) == NULL; status = sscanf (buf, "%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"); } hope this will help --
|
Mon, 05 Mar 2001 03:00:00 GMT |
|
 |
gartner e #3 / 9
|
 Invalid input in text file
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 > --
The solution to Your problem is simple: I've never used fscanf and scanf. Use fgets followed by an sscanf ! char buffer[as much you need]; eof = (fgets (buffer,sizeof(buffer),inFilePtr) == NULL); status = sscanf (buffer, "%4d%c%c", &tag_num, &enter_exit, &cr); /* check status to confirm fscanf is successful */ if (status != 3 || cr != '\n') ... --
|
Mon, 05 Mar 2001 03:00:00 GMT |
|
 |
Herb Illfelde #4 / 9
|
 Invalid input in text file
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 > --
Unless you HAVE to use fscanf, uses fgets to read a line of data into a string. Then programmically decode the string with any error checking you want. I often use strtok to parse the string into tokens and interprete the tokens to values. String to value conversion routine and character typing commands are in abundence. There are some (like strtol i think) where the value of the string and the adress of the first unrecognized character is returned. --
|
Mon, 05 Mar 2001 03:00:00 GMT |
|
 |
Sean E. Per #5 / 9
|
 Invalid input in text file
[Posted and mailed]
Quote: > 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"); > }
Frustrated, consider reading the number and then whatever is after it. Then error check the string you read. if( enter_exit != 'a' || enter_exit != 'd') do_something(); also you are only reading 4 numbers. What if the input was 1, 2, or 10 numbers? That is another source of invalid data. -- Can't buy what I want because it's free. -- Pearl Jam --
|
Wed, 07 Mar 2001 03:00:00 GMT |
|
 |
Fritz W Feuerbach #6 / 9
|
 Invalid input in text file
Why don't you use gets() first, then you could use sscanf or parse it yourself. The format doesn't seem to be that difficult.
: 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 : --
-- --
|
Fri, 09 Mar 2001 03:00:00 GMT |
|
 |
Sunil Ra #7 / 9
|
 Invalid input in text file
Quote: >Why don't you use gets() first, then you could use sscanf or parse it >yourself. The format doesn't seem to be that difficult.
You seem to have lost an 'f' somewhere... -- "I see you have books under your arm, brother. It is indeed a rare pleasure these days to come across somebody that still reads, brother." - Anthony Burgess --
|
Sat, 10 Mar 2001 03:00:00 GMT |
|
 |
Francis Glassboro #8 / 9
|
 Invalid input in text file
Quote: >Why don't you use gets() first, then you could use sscanf or parse it >yourself. The format doesn't seem to be that difficult.
or even better, learn to use fgets() correctly (and find out what flaw gets() has) 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 --
|
Sat, 10 Mar 2001 03:00:00 GMT |
|
 |
Herb Illfelde #9 / 9
|
 Invalid input in text file
Quote:
> Why don't you use gets() first, then you could use sscanf or parse it > yourself. The format doesn't seem to be that difficult.
> : 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 > : --
> -- > --
gets is BAD, BAD, BAD --
|
Sat, 10 Mar 2001 03:00:00 GMT |
|
|
|