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. Appium Java Windows环境搭建篇

    1. 安卓SDK及配置环境变量 1.1.先下载sdk安装包:installer_r24.4.1-windows.exe 下载地址:链接: http://pan.baidu.com/s/1dEyPSa9 ...

  2. spring boot 打包成jar 包在发布到服务器上

    http://blog.csdn.net/sai739295732/article/details/49444447

  3. http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html

    http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html

  4. Spring Collections XML 配置

    List <property name="lists"> <list> <value>1</value> <ref bean= ...

  5. 持续集成基础-Jenkins(二)-搭建Jenkins环境和配置第一个Job

    安装方式一(直接启动): 1.下载最新的版本(一个 WAR 文件).Jenkins官方网址: http://Jenkins-ci.org/ 2.运行 java -jar jenkins.war(需要运 ...

  6. Web App开发入门

    WebApp与Native App有何区别呢? Native App: 1.开发成本非常大.一般使用的开发语言为JAVA.C++.Objective-C. 2.更新体验较差.同时也比较麻烦.每一次发布 ...

  7. UVA 393

    The Doors Description You are to find the length of the shortest path through a chamber containing o ...

  8. CUDA程序设计(三)

    算法设计:基数排序 CUDA程序里应当尽量避免递归,因而在迭代排序算法里,基数排序通常作为首选. 1.1 串行算法实现 十进制位的基数排序需要考虑数位对齐问题,比较麻烦.通常实现的是二进制位的基数排序 ...

  9. oracle clob like

    create table products(  productid number(10) not null,  name varchar2(255),  description CLOB); 查询语句 ...

  10. 使用 Git 和 Visual Studio Online 进行版本控制

    参考资料: 在开发计算机上设置 Git(配置.创建.克隆.添加) 关于 Git 和 Visual Studio Online 是什么请自行百度 转载请注明来源: http://www.cnblogs. ...