📣 独立日限时特惠: VIP通行证立减$68
查看: 11222| 回复: 13
跳转到指定楼层
上一主题 下一主题
收起左侧

Google: Read4k

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
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.

评分

参与人数 2大米 +80 收起 理由
smzfeng + 30 谢谢 描述很清楚!
爱丽丝和鲍勃 + 50

查看全部评分


上一篇:请教如何考虑全面corner case
下一篇:Combination sum 迭代
推荐
 楼主| Interviwer 2014-11-1 14:23:12 | 只看该作者
全局:
我自己写了一个,希望指正!
  1. int Read(char* buf, int n) {
  2.     char buffer[4096];
  3.     int count = 0;
  4.     int remain = n;
  5.     int tmp = 4096;

  6.     while(remain > 0 && tmp == 4096) {
  7.         tmp = Read4k(buffer);
  8.         if(tmp < remain) {
  9.             memcpy(buf+count, buffer, tmp);
  10.             count += tmp;
  11.             remain -= tmp;
  12.         }else {
  13.             memcpy(buf+count, buffer, remain);
  14.             return n;
  15.         }   
  16.     }   
  17.     return count;
  18. }
复制代码

评分

参与人数 1大米 +5 收起 理由
22691482 + 5 回答的很好!

查看全部评分

回复

使用道具 举报

推荐
richardzrc 2014-12-10 20:02:00 | 只看该作者
全局:
Sorry, 我犯了一个错误,没有弄清楚 Read4096的具体功能。因为即使没有了也会读取,返回0个字节。
int Read(char* buf, int n)
{
    char buffer[4097];
    int ttcnt=0;
    bool eof=0;
    while(!eof && n>0)
    {
        int curcnt=Read4096(buffer);
        if(curcnt < 4096)
            eof=1;
        ttcnt+=curcnt;
        n-=curcnt;
        memcpy(buf, buffer, curcnt);
        buf+=curcnt;
    }
    return ttcnt;
}
回复

使用道具 举报

推荐
richardzrc 2014-12-10 19:40:39 | 只看该作者
全局:
为啥 我写的 这么简单 是理解错了意思么

int Read(char* buf, int n)
{
    char buffer[4097];
    int ttcnt=0;
    while(n>0)
    {
        int curcnt=Read4096(buffer);
        ttcnt+=curcnt;
        n-=curcnt;
        memcpy(buf, buffer, cnt);
        //if(curcnt<4096)
        //    return ttcnt;
        buf+=curcnt;
    }
    return ttcnt;
}
回复

使用道具 举报

全局:
用java的路过。。
感觉这个用java写的话要改API了
回复

使用道具 举报

🔗
luzhuzeng 2014-11-2 13:24:24 | 只看该作者
全局:
Interviwer 发表于 2014-11-1 00:23
我自己写了一个,希望指正!

else 里面没有更新count
回复

使用道具 举报

🔗
 楼主| Interviwer 2014-11-2 13:36:52 | 只看该作者
全局:
luzhuzeng 发表于 2014-11-2 13:24
else 里面没有更新count

对啊,直接return了,就不用更新了
回复

使用道具 举报

🔗
luzhuzeng 2014-11-3 07:34:09 | 只看该作者
全局:
Interviwer 发表于 2014-11-1 23:36
对啊,直接return了,就不用更新了

抱歉,我看错了
回复

使用道具 举报

🔗
海拔2纳米 2014-11-4 03:42:11 | 只看该作者
全局:
还来得及吗 发表于 2014-11-2 12:29
用java的路过。。
感觉这个用java写的话要改API了

没必要啊,char *变成char[]就行了,其他都一样吧
回复

使用道具 举报

🔗
zhaoweigg 2014-11-12 08:43:16 | 只看该作者
全局:
Interviwer 发表于 2014-11-1 14:23
我自己写了一个,希望指正!

lz你这个只能call 一次吧,如果我连续 call呢:
Read(buf, 1);
Read(buf, 1);

我觉得返回应当是连续两个char 吧,按lz的这个code跑的话,两次返回的char会不会差4096?

因为Read4k()这个function 内部应当有个file pointer追踪上次读到哪了,所以每次call Read4k()都是从新地方开始
回复

使用道具 举报

🔗
 楼主| Interviwer 2014-11-12 13:26:47 | 只看该作者
全局:
zhaoweigg 发表于 2014-11-12 08:43
lz你这个只能call 一次吧,如果我连续 call呢:
Read(buf, 1);
Read(buf, 1);

嗯,确实只能call一次,如果多次,就要建一个class
回复

使用道具 举报

🔗
王可雪 2014-11-12 14:15:47 | 只看该作者
全局:
本帖最后由 王可雪 于 2014-11-12 14:24 编辑

buffer是干什么用的?把文件之前的内容重置吗?我懂了,最后一次,可能读的少,要往回调。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表