Skip to content

Commit 405f9cf

Browse files
authored
Update read-n-characters-given-read4.cpp
1 parent 656c0ac commit 405f9cf

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

C++/read-n-characters-given-read4.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ class Solution {
1313
int read(char *buf, int n) {
1414
int read_bytes = 0;
1515
char buffer[4];
16-
while (int size = read4(buffer)) {
17-
size = min(size, n - read_bytes);
18-
for (int i = 0; i < size; ++i) {
19-
buf[read_bytes++] = buffer[i];
16+
for (int i = 0; i <= n / 4; ++i) {
17+
if (int size = read4(buffer)) {
18+
size = min(size, n - read_bytes);
19+
for (int j = 0; j < size; ++j) {
20+
buf[read_bytes++] = buffer[j];
21+
}
22+
} else {
23+
break;
2024
}
2125
}
2226
return read_bytes;

0 commit comments

Comments
 (0)