im using the fscanf function to handle input. now in the input each line starting with a # must be ignored. how do i ignore a complete line? for example this input:
#add some cars
car add 123456 White_Mazda_3 99 0
car add 123457 Green_Mazda_3 101 0
car add 111222 Red_Audi_TT 55 1200
#let see the cars
report available_cars
#John Doe takes a white mazda
customer new 123 JohnDoe
customer rent 123 123456
#Can anyone else take the mazda?
report available_cars
#let see Johns status
report customer 123
as you see the comments may vary in length and the commands are varying in their structure... is there some way to distinguish between two lines? or a way to tell when we are at the end/start of a line?
instead of using fscanf()
, read lines with fgets()
and use sscanf()
to replace the fscanf()
.
char s1[13], s2[4], s3[17], s4[43];
char line[1000];
while (fgets(line, sizeof line, stdin)) {
if (*line == '#') continue; /* ignore comment line */
if (sscanf(line, "%12s%3s%16s%42s", s1, s2, s3, s4) != 4) {
/* handle error */
} else {
/* handle variables */
}
}