linux read 和write的程序帮我逐条解释一下,新人不懂。 一定要详细些。

#include <unistd.h>
#include <fcntl.h>

int main()
{
int fdin, fdout;
ssize_t nread;
char buffer[1024];

fdin = open("temp1.txt", O_RDONLY);
fdout = open("temp2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);

while((nread = read(fdin, buffer, 1024)) > 0)
{
if(write(fdout, buffer, nread) < nread)
{
close(fdin);
close(fdout);
}
}

close(fdin);
close(fdout);
}

#include <unistd.h>//引入头文件LINUX/UNIX下的,为函数read/write用
#include <fcntl.h>//为函数open用

int main()
{
int fdin, fdout;//定义文件描述符一个文件进的,一个出的
ssize_t nread;//定义字节大小
char buffer[1024];//定义缓冲区字节大小

fdin = open("temp1.txt", O_RDONLY);//打开当前目录下的文件temp1.txt并且以只读方式打开
fdout = open("temp2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
//打开当前目录下的文件temp2.txt,以写方式打开,并且是没有就创建,有的话就全部覆盖掉,0644是该文件的权限 就是rwx(读/写/执行),应该明白撒
while((nread = read(fdin, buffer, 1024)) > 0)//从文件描述符里读取数据到buffer里,当读取到的字节大于0时,言外之意就是要读取完文件temp1.txt
{
if(write(fdout, buffer, nread) < nread)//把从文件temp1.txt里读到的数据一直往文件temp2.txt里写,直到写完全部temp1.txt的数据
{
close(fdin);//关闭文件描述符
close(fdout);//关闭文件描述符
}
}

close(fdin);关闭文件描述符
close(fdout);关闭文件描述符

/*基于程序的完整性,最好加上返回语句 return 0;//表示程序正常结束*/
}

不知道够不够详细呢兄弟····
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-11
#include <unistd.h>
#include <fcntl.h>

int main()
{
int fdin, fdout;
ssize_t nread;
char buffer[1024];

fdin = open("temp1.txt", O_RDONLY); //以只读方式打开文件temp1.txt,获得句柄fdin
fdout = open("temp2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);//以只写方式打开文件temp2.txt,获得句柄fdout

while((nread = read(fdin, buffer, 1024)) > 0) //通过句柄fdin从temp1.txt中读取数据到buffer数组中,读到的数据量大小为nread个字节,如果nread>0则表示能读到,所以还要继续循环
{
if(write(fdout, buffer, nread) < nread) //把buffer数组中数据写进fdout句柄代表的temp2.txt中
{
close(fdin); //关闭fdin句柄
close(fdout);//关闭fdout句柄
}
}

close(fdin);
close(fdout);
}
第2个回答  2011-04-11
逐条????
什么叫逐条。。。。。
您能说明白点么。。。。
如果是笼统点的话。或许能帮你解答。
但是如果详细的话。。。。
我想解释完了。您也用不着了
相似回答