Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top