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. (转)set集合的用法

    原地址:http://blog.csdn.net/chaiwenjun000/article/details/50561775 SET 集合 百度百科中说集合中的元素有三个特征: 1.确定性(集合中的 ...

  2. AngularJS 学习之事件

    1.ng-click指令:定义了AngularJS点击事件 <div ng-app="" ng-controller="myCtrl"> <b ...

  3. 【转】linux network namespace 学习

    原文地址:https://segmentfault.com/a/1190000004059167 介绍 在专业的网络世界中,经常使用到Virtual Routing and Forwarding(VR ...

  4. PLSQL看oracle中汉字显示乱码

    首先执行语句 select * from V$NLS_PARAMETERS  查看第一行中PARAMETER项中为NLS_LANGUAGE 对应的VALUE项中是否为SIMPLIFIED CHINES ...

  5. [linux]crontab 命令执行问题

    在服务器上设置了一个R脚本的crontab任务,死活不执行.在网上搜了很久,终于解决了. 这里主要说一下crontab异常时,该如何排查. 假设cron命令为:* * * * Rscript /you ...

  6. js的function

    1.funciton的js如果直接写是不会执行的 例如 function TableInit() { //var treeNode = $('#otherTree').omTree('getSelec ...

  7. Codeforces Round #244 (Div. 2) A. Police Recruits

    题目的意思就是找出未能及时处理的犯罪数, #include <iostream> using namespace std; int main(){ int n; cin >> ...

  8. ajax操作时用于提高用户体验的两段备用代码

    <div id="msgBoxDIV" style="position: absolute; width: 50%; padding-top: 2px; heigh ...

  9. FS_11C14温湿度传感器(二)

    作者:刘老师,华清远见嵌入式学院讲师. 在FS_11C14平台DHT11传感器程序: /******************************************************** ...

  10. Js中的window.parent ,window.top,window.self 详解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...