Ç读取字节块文件 [英] C read file in byte chunks

查看:158
本文介绍了Ç读取字节块文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到这个适当的文件,但我想读使用文件的* nix系统调用read()。我想读取的文件在1024字节块。不知道我下面的正确与否:

Could not find proper documentation on this but I am trying to read a file using *nix system call read(). And I want read the file in 1024 byte chunks. Not sure what I have below is correct or not:

while (read(fd, buffer+i, 1024) == 1){
            i++;
}

有人请验证?

推荐答案

那么,如果你不能使用,为什么不寻找的

Well if you can't use man, why not just search for it?

反正你正在使用它错了。如果您想通过块读它,你应该做这样的

Anyway you are using it wrong. If you want to read it by chunks you should do it like this

// consider that we allocated enough memory for buffer
// and buffer is byte array
ssize_t r = 0, i = 0;
do {
  r = read( fd, buffer + i, 1024 ); // try to read 1024 bytes
  i += r; 
} while( r > 0 ); 

这篇关于Ç读取字节块文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆