You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a file with enough size to read, but when I use stdin().read(...), I found the read size it returned has wrong result.
Code:
fnmain(){letmut f = stdin();letmut buf:[u8;3] = [0,0,0];letmut c = 0;loop{let i = f.read(&mut buf).unwrap();if i == 0{break;}println!("{} {}", i, c);
c += 1;}}
Command: cat some-file | ./main
The output will look like this:
3 1
3 2
3 3
... Omit a lot of 3s
3 2729
2 2730 // here: it reads only two bytes but not the expected 3
read is allowed to return smaller values than the buffer you passed in, even if more data could be read. Use read_exact if you need your buffer to be filled completely.
I have a file with enough size to read, but when I use
stdin().read(...)
, I found the read size it returned has wrong result.Code:
Command:
cat some-file | ./main
The output will look like this:
Command:
cat some-file | ./main | grep "2 "
Output:
I don't know whether it's a bug or there is another right way to read bytes from stdin so that the case above can be avoided.
Rustc version: 1.51.0
The text was updated successfully, but these errors were encountered: