Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C read() adds strange characters

Tags:

c

posix

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?

like image 447
zlenyk Avatar asked Dec 18 '13 22:12

zlenyk


1 Answers

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);
like image 112
Gilles 'SO- stop being evil' Avatar answered Oct 05 '22 23:10

Gilles 'SO- stop being evil'