Given API:
int Read4096(char* buf);
It reads data from a file and records the position so that the next time when it is called it read the next 4k chars (or the rest of the file, whichever is smaller) from the file.
The return is the number of chars read.

Todo: Use above API to Implement API
"int Read(char* buf, int n)" which reads any number of chars from the file.

我大概会这么写。

第二种写法是注意到尽量避免内存拷贝。

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int read4096(char* buf) {
static int count = ;
if (count >= ) {
count -= ;
return ;
} else {
int tmp = count;
count = ;
return tmp;
}
} class Reader {
public:
Reader():remain(), size() {}
int readN(char* buf, int n) {
int readCount = ;
while (readCount < n) {
if (remain >= n - readCount) {
memcpy(buf + readCount, tmp + size - remain, n - readCount);
remain -= (n - readCount);
readCount = n;
} else {
if (remain > ) memcpy(buf, tmp + size - remain, remain);
readCount += remain;
remain = size = read4096(tmp);
if (size == ) break;
}
}
return readCount;
} // should be more efficient, avoid some memory copy
int readN2(char* buf, int n) {
if (remain >= n) {
memcpy(buf, tmp + size - remain, n);
remain -= n;
return n;
}
int readCount = ;
if (remain > ) {
memcpy(buf, tmp + size - remain, remain);
readCount += remain;
remain = ;
}
while (readCount + <= n) {
int count = read4096(buf + readCount);
readCount += count;
if (count < ) {
return readCount;
}
}
if (readCount < n) {
size = read4096(tmp);
if (size >= n - readCount) {
memcpy(buf + readCount, tmp, n - readCount);
remain = size - n + readCount;
readCount = n;
} else {
memcpy(buf + readCount, tmp, size);
readCount += size;
}
}
return readCount;
}
private:
int remain;
int size;
char tmp[];
};
int main(int argc, char** argv) {
freopen("input.txt", "r", stdin);
Reader reader; char buff[];
int n = ;
while (true) {
int count = reader.readN2(buff, n);
cout << n << " " << count << endl;
if (count == ) break;
} return ;
}

Read4096的更多相关文章

随机推荐

  1. SpringBoot使用velocity模板引擎

    https://my.oschina.net/universsky/blog/704446

  2. 餐厅点餐APP总结

    总结:经过这几个月的时间里,我们通过学习.讨论一起做出了餐厅点餐这个APP,我们在做这个APP的过程中,每个人都有自己的想法,也通过讨论最后做出了这个app,虽然做的不是很好,但是我们也尽自己的努力尽 ...

  3. Codeforces Round #332 (Div. 2)

    水 A - Patrick and Shopping #include <bits/stdc++.h> using namespace std; int main(void) { int ...

  4. (转)STORM启动与部署TOPOLOGY

    STORM启动与部署TOPOLOGY 启动ZOOPKEEPER zkServer.sh start 启动NIMBUS storm nimbus & 启动SUPERVISOR storm sup ...

  5. HTML5 本地存储 localStorage、sessionStorage 的遍历、存储大小限制处理

    HTML5 的本地存储 API 中的 localStorage 与 sessionStorage 在使用方法上是相同的,区别在于 sessionStorage 在关闭页面后即被清空,而 localSt ...

  6. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

  7. JDBC 数据库异常 Exception 关闭的(语句,连接,ResultSet)

    如果在rs.next()之前关闭了Statement或PreparedStatement,会导致下面的异常: java.sql.SQLException: 关闭的语句: next 如果在rs.next ...

  8. oracle 函数大全及运算符

    http://blog.csdn.net/huangwuyi/article/details/7407820 一.函数 1.取整  mod(2,10)=2 2.取整 trunc(12/10)=1 3. ...

  9. 20161005 NOIP 模拟赛 T2 解题报告

    beautiful 2.1 题目描述 一个长度为 n 的序列,对于每个位置 i 的数 ai 都有一个优美值,其定义是:找到序列中最 长的一段 [l, r],满足 l ≤ i ≤ r,且 [l, r] ...

  10. BZOJ3653: 谈笑风生

    Description 设T 为一棵有根树,我们做如下的定义:• 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道高明到哪里去了”.• 设a 和 b 为 T 中的两个不同节点.如 ...