6

I've got such code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
int main(){
    char input[100];
    while(1){
        int k = read(0,input,100);
        if(k == 0) break;
        write(1,input,strlen(input));
    }
}

And after putting some lines on stdin, like:
example example
example example
...

it it's not normally displayed. Instead, near the ends of input block there are always some strange characters. Could someone explain that?

1
  • 1
    you are missing the nulls Dec 18, 2013 at 22:05

2 Answers 2

10

read reads binary data. It reports the exact number of bytes that it reads. It doesn't do anything other than read these bytes.

C strings don't contain a record of their length. The end of a string is indicated by a zero byte.

So when read reports that it read k bytes, that's exactly the number of bytes it wrote to input. It doesn't add a zero byte, so what you have in input is not a string: it's an array of bytes, of which only the first k are desired.

To print out those bytes, pass the length of the data to write. Since you want to print out the bytes that read read, pass the value returned from read.

int k = read(0,input,100);
if(k <= 0) break;
write(1, input, k);

If you want to use those bytes as a string, you need to append a trailing null byte. Note that if the input itself contains null bytes, the first of these will be the end of the string.

int k = read(0,input,99);  /*1 less to make room for the null terminator*/
if (k <= 0) break;
input[k] = 0;
fputs(input, stdout);
0
7

There is no guarantee that the data you read will be nul terminated. If it isn't, your write call will write data beyond the end of what was received, stopping only when it finds the first 0 byte, or when it crashes.

You know that input holds k bytes of data so

write(1,input,strlen(input));

should be

write(1,input,k);
1
  • "no guarantee" is putting it mildly. read() does not insert null terminators, so there will only be one if there is one in the original data, within the data obtained by this particular call to read() - and there's no guarantee that it will be at the end of the data obtained. Dec 18, 2013 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.