1、打印打开文件的描述符
code:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> int main(int argc,char * argv[]) { int fp1,fp2; printf("no is/t%d/n",fileno(stdin)); printf("no is/t%d/n",fileno(stdout)); printf("no is/t%d/n",fileno(stderr)); if((fp1=open("doc1",O_WRONLY))==-1) { perror("open"); exit(EXIT_FAILURE); } if((fp2=open("doc2",O_WRONLY))==-1) { perror("open"); exit(EXIT_FAILURE); } printf("%d/n",fp1); printf("%d/n",fp2); close(fp1); close(fp2); return 0; }
结果:
no is 0
no is 1
no is 2
3
4
2、使用 fdopen
首先使用open返回一个文件描述符,然后调用fdopen为其添加一个流对象
然后 fprintf,打印
code:
#include<sys/stat.h> #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<stdlib.h> int main(void) { int fd; FILE * stream; fd=open("doc1",O_CREAT|O_WRONLY,S_IREAD|S_IWRITE); stream=fdopen(fd,"w"); if(stream==NULL){ puts("dfopen failed"); } else{ fprintf(stream,"love the world/n"); fclose(stream); } close(fd); printf("the context is:/n"); system("cat doc1"); return 0; }
3、使用POSIX IO 实现文件拷贝
code:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<fcntl.h> #include<sys/types.h> int main(int argc,int * argv[]) { int fd_src,fd_des; char buf[128]; int num; if((fd_src=open(argv[1],"O_RONLY"))==-1){ perror("open"); exit(EXIT_FAILURE); } if((fd_des=open(argv[2],O_CREAT|O_EXCL|O_WRONLY,0644))==-1){ perror("open2"); exit(EXIT_FAILURE); } do{ num=read(fd_src,buf,128); write(fd_des,buf,num); }while(num==128); close(fd_src); close(fd_des); return 0; }
运行:./run3 doc1 doc3
4、利用lseek函数,实现文件定问,把内容写到指定的地方
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdlib.h> char buf1[]="0123456789"; char buf2[]="ABCDEFGHIJ"; char buf3[]="abcdefghij"; int main() { int fd; if((fd=creat("doc4",0644))<0){ perror("create"); exit(EXIT_FAILURE); } if(write(fd,buf1,10)!=10){ perror("write"); exit(EXIT_FAILURE); } if(lseek(fd,20,SEEK_SET)==-1){ perror("lseek"); exit(EXIT_FAILURE); } if(write(fd,buf2,10)!=10){ perror("write"); exit(EXIT_FAILURE); } if(lseek(fd,10,SEEK_SET)==-1){ perror("lseek"); exit(EXIT_FAILURE); } if(write(fd,buf3,10)!=10){ perror("write"); exit(EXIT_FAILURE); } return 0; }
结果:0123456789abcdefghijABCDEFGHIJ
5、列出某目录下 非隐藏文件基本信息
code:
#include<stdio.h> #include<stdlib.h> #include<dirent.h> int main(int argc,char * argv[]){ DIR* dirp; struct dirent * dp; dirp=opendir(argv[1]); while((dp=readdir(dirp))!=NULL){ if(dp->d_name[0]=='.') continue; printf("inode is %d/t",dp->d_ino); printf("reclen is %d/t",dp->d_reclen); printf("name is %s/n",dp->d_name); } closedir(dirp); return 0; }
结果:
inode is 1045265 reclen is 24 name is run2
inode is 1045138 reclen is 24 name is run4
inode is 1044932 reclen is 24 name is doc3
inode is 1045114 reclen is 24 name is doc1
inode is 1045109 reclen is 32 name is test3.c~
inode is 1045203 reclen is 32 name is doc1~
inode is 1045111 reclen is 32 name is test3.c
inode is 1045143 reclen is 32 name is test5.c
inode is 1045149 reclen is 24 name is run5
inode is 1045181 reclen is 32 name is test1.c~
inode is 1045184 reclen is 32 name is test1.c
inode is 1045202 reclen is 32 name is test2.c~
inode is 1044933 reclen is 32 name is test2.c
inode is 1045183 reclen is 24 name is doc2
inode is 1045187 reclen is 24 name is run
inode is 1045130 reclen is 24 name is doc4
inode is 1045132 reclen is 32 name is test4.c
inode is 1045135 reclen is 32 name is test5.c~
inode is 1045118 reclen is 24 name is run3
6、操作工作路径:
code:
#include<dirent.h> #include<stdio.h> #include<stdlib.h> int main(int argc,char * argv[]) { char * p; p=getcwd(NULL,128); printf("current path is %s/n",p); free(p); chdir("/home"); printf("new path is:%s/n",get_current_dir_name()); return 0; }
结果:
root@ubuntu:/code/chap5# ./run6
current path is /code/chap5
new path is:/home