linux下C语言文件操作相关函数
- 读取(open)文件并写入(write)另一个文件
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<fcntl.h>.h> int main() { int fd1=open("/home/cpc/Documents/diary",O_RDWR); printf("%d\n",fd1); int fd2=open("/home/cpc/Documents/anothernote",O_WRONLY|O_CREAT,0664); char buf[4096]; int len=read(fd1,buf,sizeof(buf)); while(len>0) { write(fd2,buf,len); len=read(fd1,buf,sizeof(buf)); } return 0; }
- lseek动态扩展文件
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<fcntl.h>.h> int main() { int fd1=open("/home/cpc/Documents/diary",O_RDWR|O_CREAT,0664); if(fd1==-1) { perror("file read error"); exit(1); } lseek(fd1,1000,SEEK_END);//让指针扫荡到文件末尾,再偏移1000个字节,实际上就是动态扩展 return 0; }
.......然而通过linux shell命令ll查看文件信息,文件大小并未改变
.......原来还得添加一个字符作为收尾
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<fcntl.h>.h> int main() { int fd1=open("/home/cpc/Documents/diary",O_RDWR|O_CREAT,0664); if(fd1==-1) { perror("file read error"); exit(1); } lseek(fd1,1000,SEEK_END); write(fd1,"c",1); close(fd1); return 0; }
相关推荐
penkgao 2020-10-13
penkgao 2020-06-25
lsfreeing 2020-06-20
Biao 2020-06-17
yunfeitian 2020-06-13
AaronPlay 2020-06-13
chensen 2020-11-14
拉斯厄尔高福 2020-11-04
杜倩 2020-10-29
拉斯厄尔高福 2020-10-19
嵌入式资讯精选 2020-10-15
zhaochen00 2020-10-13
yiyilanmei 2020-10-05
wanshiyingg 2020-09-29
Mars的自语 2020-09-27
shenwenjie 2020-09-24
一个逗逗 2020-09-22
flycony 2020-09-13
zhaochen00 2020-08-20