linux文件操作--学习笔迹1

5 篇文章 0 订阅

不带缓冲区的I/O操作

1、open函数需要包含的头文件,以及函数的定义:

   #include <sys/types.h>
   #include <sys/stat.h>
   #include <fcntl.h>

   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);

pathname: 文件路径字符串。
打开方式(flags):
O_RDWR(可读可写)
O_RDONLY(只读)
O_WRONLY(只写)
前面三个使用是互斥的。
可以用‘|’与O_CREAT使用,表示打开文件,如果没有则创建。
权限(mood)只有在创建文件时生效
返回值:-1表示失败。
例1:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "stdio.h"
int main()
{
        int fd;
        fd=open("./file1",O_RDWR);
        if(fd==-1)
        {
                printf("open file1 failed\n");
                 fd=open("./file1",O_RDWR|O_CREAT,0600);
                 if(fd>0){
                        printf("create file1 success\n");
                }
        }
        return 0;
}   

2、write函数头文件及函数定义:

       #include <unistd.h>

       ssize_t write(int fd, const void *buf, size_t count);

参数fd为打开的文件流。
buf:针类型,指定的内存。
count:写入的字节数。
返回值:正常则返回字节数,返回1则表示有权限被禁止。

3、read函数头文件及函数定义:

       #include <unistd.h>

       ssize_t read(int fd, void *buf, size_t count);

从fd中读取到buf,读取count个字节。
返回值:正常返回读到的字节数,返回-1表示错误。

4、lseek数头文件及函数定义:

       #include <sys/types.h>
       #include <unistd.h>

       off_t lseek(int fd, off_t offset, int whence);

offset:位移
whence:位置
参数类型有:SEEK_SET文件开头。
SEEK_CUR当前位置。
SEEK_END文件尾部。
返回值:成功返回距离开头多少字节,失败返回-1.
例2:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "stdio.h"
#include <unistd.h>
#include "string.h"
#include "stdlib.h"
int main()
{
        int fd;
        char *buf="ss hen shuai";
        fd=open("./file1",O_RDWR);
        if(fd==-1)
        {
                printf("open file1 failed\n");
                 fd=open("./file1",O_RDWR|O_CREAT,0600);
                 if(fd>0){
                        printf("create file1 succes\n");
                }
        }
        printf("open success fd=%d",fd);
        int n_write = write(fd,buf,strlen(buf));
        if(n_write!=1)
        {
                printf("write %d to file1 \n",n_write);
        } 
//      close(fd);      
//      fd=open("./file1",O_RDWR);
        char *readbuf; 
        readbuf =(char *)malloc(sizeof(char)*n_write+1);
        lseek(fd,0,SEEK_SET);
        int n_read=read(fd,readbuf,n_write);
        printf("read %d,context:%s\n",n_read,readbuf);
        close(fd);
        return 0;
}

                                

但是对文件的修改会覆盖文件里面的内容,可以在open的时候加入O_APPEND来追加到后面,或者用O_TRUNC清除原有内容。

5、实现cp指令

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "stdio.h"
#include <unistd.h>
#include "string.h"
#include "stdlib.h"
int main(int argc,char **argv)
{
        int fdsrc;
        int fdes;
        char *readbuf=NULL;
        if(argc!=3)
        {
                printf("par arm error\n");
                exit(-1);
        }
        fdsrc=open(argv[1],O_RDWR);
        int size=lseek(fdsrc,0,SEEK_END);
        lseek(fdsrc,0,SEEK_SET);
        readbuf=(char *)malloc(sizeof(char)*size+8);
        int n_read=read(fdsrc,readbuf,size);
        fdes=open(argv[2],O_RDWR|O_TRUNC|O_CREAT,0600);//清除原本内容
        int n_write = write(fdes,readbuf,strlen(readbuf));
        close(fdsrc);
        close(fdes);
        return 0;
}

文件配置

1、修改文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "stdio.h"
#include <unistd.h>
#include "string.h"
#include "stdlib.h"
int main(int argc,char **argv)
{
        int fdsrc;
        int fdes;
        char *readbuf=NULL;
        if(argc!=2)
        {
                printf("par arm error\n");
                exit(-1);
        }
        fdsrc=open(argv[1],O_RDWR);
        int size=lseek(fdsrc,0,SEEK_END);
        lseek(fdsrc,0,SEEK_SET);
        readbuf=(char *)malloc(sizeof(char)*size+8);
        int n_read=read(fdsrc,readbuf,size);
        char *p=strstr(readbuf,"LENG=");//返回找到串开头的位置
        if(p==NULL)
        {
                printf("not found\n");
                exit(-1);
        }
        p=p+strlen("LENG=");
        *p='5';
        lseek(fdsrc,0,SEEK_SET);
        int n_write = write(fdsrc,readbuf,strlen(readbuf));
        close(fdsrc);

        return 0;
}

2、写入一个结构体

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "stdio.h"
#include <unistd.h>
#include "string.h"
#include "stdlib.h"
struct Test
{
        int a;
        char c;
};
int main()
{
        int fd;
        struct Test data1 ={100,'a'};
        struct Test data2;
        fd=open("./file1",O_RDWR);
        int n_write = write(fd,&data1,sizeof(struct Test));
        lseek(fd,0,SEEK_SET);
        int n_read=read(fd,&data2,sizeof(struct Test));
        printf("read %d,%c",data2.a,data2.c);
        close(fd);
        return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值