n****e 发帖数: 678 | 1 Implement readline using read4096
这道题之前版上有讨论过,不知道有没有标准的解答codes
网上搜了搜,感觉网上贴的codes都不太对。
多谢! |
h**o 发帖数: 548 | 2 你怎么做的
【在 n****e 的大作中提到】 : Implement readline using read4096 : 这道题之前版上有讨论过,不知道有没有标准的解答codes : 网上搜了搜,感觉网上贴的codes都不太对。 : 多谢!
|
d**********x 发帖数: 4083 | 3 i think the standard way should be to maintain an internal buffer.
what's the solutions from internet?
【在 n****e 的大作中提到】 : Implement readline using read4096 : 这道题之前版上有讨论过,不知道有没有标准的解答codes : 网上搜了搜,感觉网上贴的codes都不太对。 : 多谢!
|
h**o 发帖数: 548 | 4 贴个我的吧:
#define FOURK 4096
class readbuf{
readbuf(){nsize = 0; ptr = 0;}
char buf[FOURK];
int ptr;
int nsize;
int readN(char*dst, int n);
int readLine(string& s);
};
int readbuf::readN(*dst, int n){
if (!dst) return 0;
int bytes = 0;
while(n>0){
if (nsize==0) {nRead = read4K(buf); nsize = nRread; ptr = 0;}
min = min(n, nsize);
bytes+=memcpy(dst+bytes, buf, min);
ptr+=min; n-=min; nsize-=min;
if (nRead0 or ==0, need to break because
nothing to be read4K()
}
return bytes;
}
int readbuf::readLine(string& s){
int bytes = 0; bool endline = false;
while(1){
if (nsize==0) {nRead = read4K(buf); nsize = nRread; ptr = 0;}
for(; ptr< FOURK && nsize>0;ptr++, nsize--) {
s.append (buf[ptr]); bytes++;
if (buf[ptr] == ' \ 0' || buf[ptr] == ' \ n ') endline=true;
}
if (endline || nRead
}
}
【在 d**********x 的大作中提到】 : i think the standard way should be to maintain an internal buffer. : what's the solutions from internet?
|
s**x 发帖数: 7506 | 5 readN is not called anywhere?
【在 h**o 的大作中提到】 : 贴个我的吧: : #define FOURK 4096 : class readbuf{ : readbuf(){nsize = 0; ptr = 0;} : char buf[FOURK]; : int ptr; : int nsize; : int readN(char*dst, int n); : int readLine(string& s); : };
|
h**o 发帖数: 548 | 6 usually there are two FAQ about this topic, sometimes readN(char* dst, int
size) is asked during interview, given read4096(char* buf); sometimes
readLine(char* dst) is asked, given read4096(char* buf). I am writing both.
【在 s**x 的大作中提到】 : readN is not called anywhere?
|
s**x 发帖数: 7506 | 7 I see.
"bytes+=memcpy(dst+bytes, buf, min); "
buf should be ptr here?
【在 h**o 的大作中提到】 : usually there are two FAQ about this topic, sometimes readN(char* dst, int : size) is asked during interview, given read4096(char* buf); sometimes : readLine(char* dst) is asked, given read4096(char* buf). I am writing both.
|
h**o 发帖数: 548 | 8 yeah. bytes+=memcpy(dst+bytes, buf+ptr, min);
thanks
【在 s**x 的大作中提到】 : I see. : "bytes+=memcpy(dst+bytes, buf, min); " : buf should be ptr here?
|
n****e 发帖数: 678 | 9 Implement readline using read4096
这道题之前版上有讨论过,不知道有没有标准的解答codes
网上搜了搜,感觉网上贴的codes都不太对。
多谢! |
h**o 发帖数: 548 | 10 你怎么做的
【在 n****e 的大作中提到】 : Implement readline using read4096 : 这道题之前版上有讨论过,不知道有没有标准的解答codes : 网上搜了搜,感觉网上贴的codes都不太对。 : 多谢!
|
|
|
d**********x 发帖数: 4083 | 11 i think the standard way should be to maintain an internal buffer.
what's the solutions from internet?
【在 n****e 的大作中提到】 : Implement readline using read4096 : 这道题之前版上有讨论过,不知道有没有标准的解答codes : 网上搜了搜,感觉网上贴的codes都不太对。 : 多谢!
|
s**x 发帖数: 7506 | 12 readN is not called anywhere?
【在 h**o 的大作中提到】 : 贴个我的吧: : #define FOURK 4096 : class readbuf{ : readbuf(){nsize = 0; ptr = 0;} : char buf[FOURK]; : int ptr; : int nsize; : int readN(char*dst, int n); : int readLine(string& s); : };
|
h**o 发帖数: 548 | 13 usually there are two FAQ about this topic, sometimes readN(char* dst, int
size) is asked during interview, given read4096(char* buf); sometimes
readLine(char* dst) is asked, given read4096(char* buf). I am writing both.
【在 s**x 的大作中提到】 : readN is not called anywhere?
|
s**x 发帖数: 7506 | 14 I see.
"bytes+=memcpy(dst+bytes, buf, min); "
buf should be ptr here?
【在 h**o 的大作中提到】 : usually there are two FAQ about this topic, sometimes readN(char* dst, int : size) is asked during interview, given read4096(char* buf); sometimes : readLine(char* dst) is asked, given read4096(char* buf). I am writing both.
|
h**o 发帖数: 548 | 15 yeah. bytes+=memcpy(dst+bytes, buf+ptr, min);
thanks
【在 s**x 的大作中提到】 : I see. : "bytes+=memcpy(dst+bytes, buf, min); " : buf should be ptr here?
|
y***n 发帖数: 1594 | 16 答案可以再贴一下吗?
【在 h**o 的大作中提到】 : usually there are two FAQ about this topic, sometimes readN(char* dst, int : size) is asked during interview, given read4096(char* buf); sometimes : readLine(char* dst) is asked, given read4096(char* buf). I am writing both.
|
A*****i 发帖数: 3587 | 17 上个月刚被G家问过
这个主要就是考虑边界吧,ptr没有什么tricky的问题好像 |
y***n 发帖数: 1594 | 18 readN 和 readLine 都是一个思路吗? |