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. maven工程下 读取resource下配置文件

    http://blog.csdn.net/xu511739113/article/details/52440982

  2. JQuery validate验证 自定义

    http://www.w3cschool.cc/jquery/jquery-plugin-validate.html http://blog.163.com/zhao_jinggui/blog/sta ...

  3. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  4. ROW_NUMBER() OVER函数的基本用法

    转自:http://www.cnblogs.com/icebutterfly/archive/2009/08/05/1539657.html 语法:ROW_NUMBER() OVER(PARTITIO ...

  5. 【HDU】3480 Division

    http://acm.hdu.edu.cn/showproblem.php?pid=3480 题意:一个n个元素的集合S要求分成m个子集且子集并为S,要求$\sum_{S_i} (MAX-MIN)^2 ...

  6. 【noiOJ】p1759

    1759:最长上升子序列 查看 提交 统计 提问 总时间限制:  2000ms 内存限制:  65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我 ...

  7. Android -- ImageSwitch和Gallery 混合使用

    1. 实现效果

  8. 再说virtual

    看了对Anders Hejlsberg的采访, 1)C#中函数默认是非virtual的设计因为:在java中,一个方法默认是虚拟化的,只有对一个方法必须声明final关键字,这样这个方法才是非虚的,无 ...

  9. 20145330第十周《Java学习笔记》

    20145330第十周<Java学习笔记> 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就 ...

  10. 20145330《Java程序设计》第三周学习总结

    20145330 <Java程序设计>第三周学习总结 第三周知识的难度已经逐步上升,并且一周学习两章学习压力也逐渐加大,需要更高效率的来完成学习内容,合理安排时间. 类与对象 对象(Obj ...