2.1文件IO

文件描述符特点:
内核每打开一个文件就会获取一个相应的文件描述符,反映其打开的文件的特点及属性
1.很小的非负整数
2.一个程序最多可打开1024(0-1023)个文件
3.新获取的文件描述符总是未占用的最小的非负整数

STDIN_FILENO 0
STDOUT_FILENO 1
STDERR_FILENO 2

======================================================================

open_0.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 int main(void)
 6 {
 7     int fd = 0;
 8        if(-1 == (fd = open("file.txt",O_RDONLY)))
 9         {    
10             perror("fail to open!");
11             return -1;
12         }
13     printf("fd = %d\n",fd);//打印文件描述符
14     close(fd);
15 
16     return 0;
17 }
18 /*
19   #include <sys/types.h>
20   #include <sys/stat.h>
21   #include <fcntl.h>
22   int open(const char *pathname, int flags);
23   功能:打开一个文件获取其文件描述符
24   参数:文件路径/打开方式:
25           O_RDONLY
26           O_WRONLY
27           O_RDWR
28         //O_CREAT:没有则创建
29         //O_TRUNC:清0打开
30         //O_EXCL:检测文件是否存在
31         //....
32   返回值:成功返回一个文件描述符;失败返回-1
33 
34  * */

file.txt

 1 abcccccccccccccccccccccccccccccccccccccccc 

fileno.c

 1 #include <stdio.h>
 2 //#include <sys/types.h>
 3 //#include <sys/stat.h>
 4 //#include <unistd.h>
 5 //#include <fcntl.h>
 6 #include <string.h>
 7 
 8 int main(void)
 9 {
10     int n = 0;
11     n = fileno(stdin);
12     printf("STDIN --> %d\n", n);
13     //printf("STDIN_FILENO:\n", STDIN_FILENO);
14 
15     n = fileno(stdout);
16     printf("STDOUT --> %d\n", n);
17     //printf("STDOUT_FILENO:\n", STDOUT_FILENO);
18     
19     n = fileno(stderr);
20     printf("STDERR --> %d\n", n);
21     //printf("STDERR_FILENO:\n", STDERR_FILENO);
22     
23     return 0;
24 }
25 
26 /*
27 int fileno(FILE *stream);
28 功能:将文件流指针转化成文件描述符
29 
30 
31 */

write.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 #include <unistd.h>
 6 int main(void)
 7 {
 8     int fd = 0;
 9     int ret =0;
10     char buff[1024] = "hello world!heheda!";
11        if(-1 == (fd = open("file_write.txt",O_WRONLY|O_CREAT|O_TRUNC)))
12         {    
13             perror("fail to open!");
14             return -1;
15         }
16 //    ret = read(fd,buff,1024);//从fd中读1024个到buff
17     write(fd,buff,sizeof(buff));//写sizeof(buff)个字节从buff到fd中去
18 //    printf("fd = %d\n",fd);//打印文件描述符
19     close(fd);
20 
21     return 0;
22 }

file_write.txt

 1 a 

read.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 #include <unistd.h>
 6 int main(void)
 7 {
 8     int fd = 0;
 9     int ret =0;
10     char buff[1024] = {0};
11        if(-1 == (fd = open("file.txt",O_RDONLY)))
12         {    
13             perror("fail to open!");
14             return -1;
15         }
16     ret = read(fd,buff,1024);//从fd中读1024个到buff
17     write(STDOUT_FILENO,buff,ret);//写到STDOUT_FILENO
18 //    printf("fd = %d\n",fd);//打印文件描述符
19     close(fd);
20 
21     return 0;
22 }

lseek.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 #include <unistd.h>
 6 int main(void)
 7 {
 8     int fd = 0;
 9        if(-1 == (fd = open("file_lseek.txt",O_WRONLY|O_CREAT|O_TRUNC)))
10         {    
11             perror("fail to open!");
12             return -1;
13         }
14     long len = 0;
15     char ch = 'a';
16     len = lseek(fd,1,SEEK_SET);
17     write(fd,&ch,1);//写sizeof(buff)个字节从buff到fd中去
18     printf("len = %d\n",len);
19 
20     ch = 'b';
21     len = lseek(fd,5,SEEK_SET);
22     write(fd,&ch,1);//写sizeof(buff)个字节从buff到fd中去
23     printf("len = %d\n",len);
24     
25 //    char ch = 'a';
26 //    lseek(fd,1,SEEK_SET);
27 //    write(fd,&ch,1);//写sizeof(buff)个字节从buff到fd中去
28 //    printf("len = %d\n",len);
29     
30     close(fd);
31     return 0;
32 }

file_lseek.txt

 1 a b 

fdopen.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 #include <unistd.h>
 6 
 7 int main(void)
 8 {
 9     int fd = 0;
10     FILE *fp = NULL;
11     if ( -1 == (fd = open("file_fdopen.txt",O_WRONLY | O_TRUNC | O_CREAT, 0644)))
12     {
13         perror("fail to open!");
14         return -1;
15     }
16 
17     if(NULL == (fp = fdopen(fd, "w")))
18     {
19         perror("fail to fdopen!");
20         return -1;
21     }
22 
23     fputs("hello world",fp);
24     fclose(fp);
25 
26     return 0;
27 }
28 
29 /*
30 FILE *fdopen(int fd, const char *mode);
31 功能:与已打开的文件描述符建立一个文件流指针
32 参数:mode:r  w  a
33            r+ w+ a+
34 返回值:成功返回一个文件流指针,失败则返回NULL
35 
36 */

file_fdopen.c

 1 hello world 

dup.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 #include <unistd.h>
 6 #include <string.h>
 7 
 8 int main(void)
 9 {
10     int fd = 0;
11     int newfd = 0;
12     char buff[] = "dup:hello world -->write to file_dup.txt";
13     if(-1 == (fd = open("file_dup.txt",O_WRONLY | O_TRUNC | O_CREAT,0644)))
14     {
15         perror("fail to open!");
16         return -1;
17     }
18         
19     //close(STDOUT_FILENO);  //1
20     close(1);  //1
21     newfd = dup(fd);
22     printf("fd:%d\nnewfd:%d\n",fd,newfd);
23 
24     printf("hello world ,dup!\n");
25     printf("%s\n",buff);
26     return 0;
27 }
28 
29 /*
30  int dup(int oldfd);
31  功能:拷贝一个文件描述符
32  参数:oldfd:需要拷贝的文件描述符
33  返回值:成功返回对应新的文件描述符;失败返回-1
34  
35  */

file_dup.txt

 1 fd:3 2 newfd:1 3 hello world ,dup! 4 dup:hello world -->write to file_dup.txt 

system_file.c

1 #include <stdio.h>
2 int main(char argc,const char *argv[])//主函数传参
3 {
4     system("cat system_file.txt");
5     return 0;
6 }

system_file.txt

 1 hello world! 2 i love you,liang! 3 forever! 

symlink.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 int main(void)
 4 {
 5     char buff[1024] = {0};
 6     if(-1 == (symlink("file_symlink.txt","syma.txt")))
 7     {
 8         perror("fail to symlink!");
 9         return -1;
10     }
11 
12     if(-1 == readlink("syma.txt",buff, sizeof(buff)))
13     {
14         perror("fail to readlink!");
15         return -1;
16     }
17 
18     puts(buff);         //等价表达
19     printf("%s\n",buff);//等价表达
20     return 0;
21 }
22 
23 /*
24 int symlink(const char *oldpath, const char *newpath);
25 功能:创建一个软链接文件
26 参数:oldpath:旧文件路径
27       newpath:新文件路径
28 返回值:成功返回0;失败返回-1
29 
30 ssize_t readlink(const char *path, char *buf, size_t bufsiz);
31 功能:从链接文件本身来读取信息
32 参数:path:链接文件路径
33       buf: 存放 读到链接文件信息 的地址
34       bufsiz:读取信息的大小
35 返回值:成功返回写入buf的字节;失败返回-1.
36 
37 
38 
39  */

file_symlink.txt

 1 hello c 2 welcome to our linux world! 

syma.txt

 1 hello c 2 welcome to our linux world! 

link.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 int main(void)
 4 {
 5     char buff[1024] = {0};
 6 
 7     if(-1 == unlink("linkb.txt"))
 8     {
 9         perror("fail to unlink!");
10         return -1;
11     }
12 
13     if(-1 == (link("file_symlink.txt","linkb.txt")))
14     //将file_symlink.txt看作源文件
15     {
16         perror("fail to link!");
17         return -1;
18     }
19 
20     return 0;
21 }
22 
23 /*
24 int link(const char *oldpath, const char *newpath);
25 功能:创建一个硬链接
26 参数:oldpath:旧文件路径
27       newpath:新文件路径
28 返回值:成功返回0;失败返回-1.
29 
30 int unlink(const char *pathname);
31 功能:删除一个硬链接
32 参数:path:文件路径
33 返回值:成功返回0;失败返回-1.
34     
35  */

linkb.txt

 1 hello c 2 welcome to our linux world! 

 

posted @ 2017-03-15 16:51  bkycrmn  阅读(138)  评论(0)    收藏  举报