1

I'm trying to write a C program that take input and output using system calls (read and write). However, I also want to convert using sscanf. The program will input a starting and ending value and will count to that ending value. I'm trying to use sscanf to convert.

int main(int argc, char **argv){                                                
    char beg[5];                                                                
    char stop[5];                                                               
    int i, start, end;                                                          

    write(1, "Starting value: ", 16);                                           
    read(0, beg, 4);                                                            
    sscanf(beg,"%d",&start);                                                    

    write(1, "Ending value: ", 14);                                             
    read(0, stop, 4);                                                           
    sscanf(stop,"%d",&end);                                                     


    int dif = end - start;                                                      
    if (dif >= 0){                                                              
        for (i = start; i <= end; i++){                                         
            write(1, &i, sizeof(i));                                            
        }                                                                       
        write(1, "\n", 1);                                                      
    }                                                                           
    return 0;
}

Desired output:

Starting value: 1
Ending value: 4
1 2 3 4

However, nothing prints for the count (it works if I used printf("%d ", i);)

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
Jakeeln
  • 353
  • 1
  • 4
  • 14

1 Answers1

1

You have this:

int i;
write(1, &i, sizeof(i));

I guess you believe this is equivalent to:

printf("%d", i);

But it is not!

Whereas printf() will format its arguments into human-readable ASCII text, write() does no such thing. Instead, it writes raw bytes directly in a file as binary, machine-readable data.

Just use printf().

And as for this:

write(1, "Starting value: ", 16);

That's bad code. Instead:

puts("Starting value: ");
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • @Jakeeln: Why? Is it that you are doing a school assignment and are not allowed to use `printf()`? If so, you should mention this in your question. – John Zwinck Mar 26 '17 at 01:42
  • No not for school, i meant the reason for the post was specifically to better understand the use of system calls. – Jakeeln Mar 26 '17 at 01:47
  • i guess a specific question, how would you change `write(1, &i, sizeof(i));` to print the value? – Jakeeln Mar 26 '17 at 02:05
  • @Jakeeln: For that you first need to convert the integer to a string, then `write()` it. As for how to do the first step, it's covered here: http://stackoverflow.com/questions/8257714/how-to-convert-an-int-to-string-in-c – John Zwinck Mar 26 '17 at 02:14