Here's a brand new brand for C. For example, # fragment1 ## fragment2 ## fragment3 #
I want to check the two errors before reading the program in one file in a row and then "#" by separating it. I am not sure that one piece is more than 1000 characters, and that the correct format of the file "#" does not actually get me on the sides of the piece how the fscanf syntax works, but I think the following errors will be checked:
four buffers [MAX_FRAG_LEN + 1]; If (fscanf (fp, "#% 1000 [^ #] #", buffer) == 1) {Return stdup (buffer); } And {fprintf (stderr, "error! Incorrect format. \ N"); }However, I want to separate the errors so I can specifically message one of two messages. How can I make a check personally? greatly appreciate!
To work your technique, you should manually check whether the next letter after buffer Is a
'#' after
fscanf returns. This allows you to isolate a very long string error from the missing
"##" error.
Four buffers [MAX_FRAG_LEN + 1]; If (fscanf (fp, "#% 1000 [^ #]", buffer) == 1) {int c = fgetc (fp); If (C == '#') Return Strudge (buffer); If (c! = EOF) {ungetc (c, fp); Fprintf (stderr, "error! More than 1000 characters. \"); } And {fprintf (stderr, "error! Last piece did not end with '#'. \ N"); }} And {fprintf (stderr, "Error! Piece 'did not start with' # '. \ N); }
However, it is usually difficult to use
fscanf for parsing. If the input is completely in an unexpected format, it can be difficult to recover them properly, so reading through a full line in a buffer is usually easier to handle errors, and then parse that line as Duck has been suggested.
Comments
Post a Comment