Linux系统文件读写

读写文件的操作是最平常不过的了,也是最最基础的知识了,这里主要是通过系统调用的方式来实现的,是自己学习过程中随便写的,以便做个记录,高手请飘过!

代码文件:file_opera.c

复制代码
  1 /***********************************************************
  2     功能说明:系统调用方式文件操作
  3     author: linux.sir@qq.com
  4 
  5 ***********************************************************/
  6 #include <stdlib.h>
  7 #include <unistd.h>
  8 #include <sys/types.h>
  9 #include <sys/stat.h>
 10 #include <fcntl.h>
 11 #include <errno.h>
 12 #include <stdio.h>
 13 #include <string.h>
 14 
 15 #define BUF_SIZE 1024
 16 
 17 /**********************************************************
 18 
 19     方法功能:主方法
 20 
 21 **********************************************************/
 22 int main(int argc, char * argv[])
 23 {
 24   int fd;
 25 
 26   if(argc<2)
 27   {
 28     printf("请输入文件名\n");
 29     exit(1);
 30     
 31   }
 32 
 33   fd = openfile(argv[1]);
 34   
 35   printf("open result fd --%d\n", fd);
 36 
 37   
 38 
 39   int wd = writefile(fd);
 40   
 41   int r = closefile(fd);    //前面对些文件进行写操作,游标指向末尾,后面再进行读时,需要关掉一下,重新打开文件
 42   
 43   fd = openfile(argv[1]);
 44   int rd = readfile(fd);
 45   r = closefile(fd);
 46 
 47 
 48   char dest[]="/home/magc/workspace/0124/test.bak";
 49   
 50   fd = openfile(argv[1]);
 51   int dest_fd = openfile(dest);
 52   copyfile(fd,dest_fd);
 53   
 54 }
 55 /**********************************************************
 56 
 57     方法功能:系统调用 open方法
 58 
 59 **********************************************************/
 60 int openfile(const char *pfile)
 61 {
 62 
 63   int fd;
 64   fd =  open(pfile, O_CREAT|O_RDWR|O_APPEND, S_IRWXU);
 65   if(fd==-1)
 66   {
 67     printf("open error :%m\n", errno);
 68     
 69   }
 70   return fd;
 71   
 72   
 73 }
 74 /**********************************************************
 75 
 76     方法功能:系统调用 close()方法
 77 
 78 **********************************************************/
 79 int closefile(int fd)
 80 {
 81   int r = close(fd);
 82   if(r == -1)
 83   {
 84     printf("close error for %m\n", errno);
 85     exit(1);
 86     
 87   }else
 88   {
 89     printf("close success !\n");
 90     
 91   }
 92   
 93   return r ;
 94   
 95 }
 96 int readfile(int fd)
 97 {
 98   char buf[512];
 99   
100   int len = read( fd, buf, 512);
101   if(len!=512)
102   {
103     
104     printf("read failed for %m\n", errno);
105     //  exit(-1);
106     
107   }
108   printf("read result:%s\n", buf);
109   
110   
111 }
112 /**********************************************************
113 
114     方法功能:文件写入  write()方法
115 
116 **********************************************************/
117 int writefile(int fd)
118 {
119   char buf[] = "hello tianjin\n";
120   int len = write( fd, buf, strlen(buf));
121   if(len==-1)
122   {
123     printf("write error for %m\n", errno);
124     
125   }
126   return len;
127   
128   
129 }
130 
131 /**********************************************************
132 
133     方法功能:文件复制功能(系统调用方式实现)
134             read()
135             write()
136             close()
137 
138 **********************************************************/
139 int copyfile(int src_fd , int dest_fd )
140 {
141 
142 
143   char buf[BUF_SIZE];
144   int rlen;
145   int wlen;
146   
147   while(rlen=read(src_fd,buf,BUF_SIZE))  //buf比较小,需要考虑持续读取内容 
148   {
149     if(rlen>0)
150     {
151       wlen = write(dest_fd,buf, rlen);
152       if(wlen!=rlen)
153       {
154         printf("write error for %m\n", errno);
155         exit(-1);
156         
157       }
158       
159     }
160     
161   }
162   
163   printf("copy success !\n");
164   
165   
166 }
复制代码



注意事项:

1)通过create()方法来创建文件的方法不提供使用,统一用Open方法来处理。

2)读写及权限可以写多项,用“|”来分隔即可。

3)注意理解文件描述符,它是有游标的,在第33行里,需要先关掉之前Write()方法用的文件描述符,后面再次打开一下再进行读内容。

4)注意理解用Char数组来作为缓冲区。

5)通过系统调用方式来处理文件操作和利用 C标准库实现文件操作相比,应该是标准库函数更易用,但系统调用方式也有它的用武之地,例如驱动开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值