c - Reading both strings and integers from a file -


i'm reading in text file lines begin asterisk, other lines contain numbers indicating size of rectangle. i'm confused on how should extracting information though. i'm trying use fgets() read strings , sscanf read integers, seen;

   while(fgets(line, max_chars, infile) != null) {         if(line[0] == '*') {             printf("%s\n", line);         }         sscanf(line, "%d%d", &height, &width);         printf("height: %d, width: %d\n", height, width);     } 

but it's printing this;

*case #1

height: 0, width: 0

height: 10, width: 20

....

*case #2: shape

height: 10, width: 20

height: 7, width: 7

....

should reading file character character instead?

sample input: (the height , width aren't updated right away)

 *case #1  10 20       x                 x              xxx                 *case #2: shape   7 7       xx      xx        x    

you forgot add else part after if.

while(fgets(line, max_chars, infile) != null) {     if(line[0] == '*') {         printf("%s\n", line);     }     // missing piece     else {        sscanf(line, "%d%d", &height, &width);        printf("height: %d, width: %d\n", height, width);     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -