酒类营销网站,河南做网站推广,重庆新媒体运营公司有哪些,百度收录规则用系统调用IO函数实现从一个文件读取最后2KB数据并复制到另一个文件中#xff0c;源文件以只读方式打开#xff0c;目标文件以只写的方式打开#xff0c;若目标文件不存在#xff0c;可以创建并设置初始值为0664#xff0c;写出相应代码#xff0c;要对出错情况有一定的处…用系统调用IO函数实现从一个文件读取最后2KB数据并复制到另一个文件中源文件以只读方式打开目标文件以只写的方式打开若目标文件不存在可以创建并设置初始值为0664写出相应代码要对出错情况有一定的处理并能够让用户自行输入要复制的文件名。 IO口即指Input和Outpot常用的IO函数open() close() read() write() lseek()。本节也围绕以上函数完成。
题目分析
本质从一个文件读取然后写到另一个文件里。
添加形容
源文件要读取的文件目标文件要写入的文件 源文件以只读方式打开from_writedopen(argv[1],O_RDONLY); 目标文件以只写方式打开to_readedopen(argv[2],O_WRONLY); 若目标文件不存在可以创建并设置初始值为0664to_readedopen(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664); 命令简介 O_RDONLY --以只读命令打开文件 O_WRONLY--以只写命令打开文件 O_WRONLY--以读写命令打开文件 O_CREAT--若文件不存在则创建 O_TRUNC--若文件存在则清除原有数据 最后一个参数mode 是文件权限0代表没有权限1代表可执行权限2代表可写权限4代表可读权限将其相加可得0~7的八进制数。 0664表示的是 文件所有者可读可写和文件所有者同组用户可读可写其他用户可读。 对出错情况有一定的处理
if(xx0){printf(cant open %s,argv[x]);return-1}或者ifxx0)
{perror(xx);}
(要使用perror前提引用库函数 #includeerror.h 让用户自行输入要复制的文件名 这里主函数里写入参数 int main(int argc, char *argv[]) argc用来传参此篇argc一共有三个参数除了程序名还有两个分别是源文件和目标文件“要被复制的文件”和“要复制到的文件”基于此实现用户自行输入要复制的文件名。 读取源文件最后2kb的数据 用lseek函数实现 int lenlenlseek(from_writed,-70,SEEK_END); 题目要求是最后2kb但是2048太大了我写的文件比较小我就偏移个最后70刚好是我文件最后一句话。 大家自己看想偏多少都行。 函数原型 off_t lseek(int fd , off_t offset, int whence) 先来说一下这个off_t类型吧它用于指示文件的偏移量。你可以就简单的理解为这是一个64位的整形数相当于long long int其定义在unistd.h头文件中可以查看。 参数fd //文件描述符可以通过open函数得到通过这个fd可以操作某个文件 参数: offset //文件偏移量是一个整形数 参数whence //偏移类型下列三个值中选一个。 offset offset 0 向后偏移 offset 0 向前偏移 whence : SEEK_SET:从文件头开始偏移 SEEK_CUR:从当前位置开始偏移 SEEK_END:从文件尾开始偏移 在使用这个函数之前我们需要往C/C文件中导入这些头文件 #include sys/types.h #include unistd.h 到此题目就分析完了。
写代码
先创个源文件
vi LLL IO实现文件全部复制
#include stdio.h
#include stdlib.h
#include unistd.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include error.h
int main(int argc, char *argv[])
{if(argc ! 3){printf(Input error\n);exit(1);}int fp_from open(argv[1], O_RDONLY);//只读if(fp_from-1){printf(open %s error\n,argv[1]);exit(2);}int fp_to open(argv[2], O_WRONLY | O_CREAT, 0644);//读写不存在则创建if(fp_to-1){printf(open %s error\n,argv[2]);exit(3);}int buf[1024] {0};ssize_t ret;while(1){ret read(fp_from,buf,sizeof(buf) - 1);//从源文件读if(ret -1){perror(read);}else if(ret 0){printf(拷贝完毕\n);break;}ret write(fp_to,buf,ret); //向目标文件写if(ret -1){perror(write);}}close(fp_from);close(fp_to);return 0;
}
运行结果 利用光标偏移IO实现文件部分复制
#includestdio.h
#includestdlib.h
#includeunistd.h
#includesys/types.h
#includesys/stat.h
#includefcntl.h
int main(int argc,char **argv)
{ int from_writed; //源文件int to_readed; //目标文件int l_writed; //写入长度int l_readed; //读取长度int buf[2048];int len; //偏移长度if(argc ! 3){printf(Input error\n);return -1;}from_writedopen(argv[1],O_RDONLY);//以只读方式打开源文件if(from_writed0){printf(cant oppen %s\n,argv[1]);return -1;}to_readedopen(argv[2],O_WRONLY);//以只写方式打开目标文件if(to_readed0) //如果文件不存在{to_readedopen(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664);//读写方式|不存在则创建|存在则清除数据权限0664if(to_readed0){printf(cant open %s\n,argv[2]);return -1;}}while(1){// off_t len;lenlseek(from_writed,-70,SEEK_END); //移动光标至文件末尾前70个数据if(len0){printf(current off_t : %d\n,len);输出当前光标位置所在}l_readedread(from_writed,buf,2048); //从源文件读取if(l_readed0){printf(cant read from %s\n,argv[1]);return -1;}if(l_readed0){l_writedwrite(to_readed,buf,l_readed);//写入到目标文件if(l_writed0){printf(拷贝完毕\n);break;}if(l_writed0){printf(cant write to %s\n,argv[2]);return -1;}}elsebreak;}close(from_writed);close(to_readed);return 0;}
还利用上面那个LLL文件 运行 查看 刚好偏移最后一句话。 文章转载自: http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.xmnlc.cn.gov.cn.xmnlc.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.jmmzt.cn.gov.cn.jmmzt.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.mgfnt.cn.gov.cn.mgfnt.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn