Skip to content

Commit 656c0ac

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

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ class Solution {
1313
int read(char *buf, int n) {
1414
int read_bytes = 0;
1515
char buffer[4];
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;
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];
2420
}
2521
}
26-
return min(read_bytes, n);
22+
return read_bytes;
2723
}
2824
};

0 commit comments

Comments
 (0)