Вопрос

I'm trying to read in user input for a file name, to which I then attempt to open the file specified. The problem I'm getting is that there seems to be junk characters added on to the end of the input. (I discovered this when trying to printf() the userInput variable).

if(read(0, userInput, 128) < 0)
    write(2, "An error occurred in the read.\n", 31); 

Any idea what's going wrong? I got this code right from codewiki Thanks

Это было полезно?

Решение

You haven't posted enough info to post a definite answer, but here's my educated guess:

C strings are null terminated. If you read only a partial string and don't terminate it, but try to print it as a C string, you'll get undefined behaviour - in practice, you'll see the garbage following it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top