C: ignore comment line in input file

im using the fscanf function to handle input. now every line starting with a # character should be ignored in the input. how do i ignore the full 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 

since you see that comments can vary in length and commands vary in structure ... is there a way to distinguish between two lines? or a way to tell when we are at the end / beginning of a line?

+4
source share
2 answers

instead of fscanf() , read the lines with fgets() and use sscanf() to replace 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 */ } } 
+4
source

Use fgets() instead of fscanf() for line input instead of entering free-form.

+2
source

All Articles