Linux文件编程(1)

本文详细介绍了Linux系统中的open函数使用,包括O_EXCL、O_APPEND标志的含义和示例。同时讲解了creat函数用于创建新文件的用法。接着,展示了如何编写一个简单的CP命令来复制文件,以及如何修改配置文件的内容。最后,演示了如何将整数和结构体写入文件。
摘要由CSDN通过智能技术生成

一、open函数的细节补充

1、 O_EXCL 如果同时指定了O_CREAT,而文件已经存在,则返回-1;

 fd= open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
      if(fd == -1){
        printf("fd = %d,file alrealy exists\n",fd);
        return 0;
      }


2、O_APPEND 每次写时都加到文件尾端;

fd= open("./file1",O_RDWR|O_APPEND);

3、O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或者
     只写成功打开,则将其长度截短为0。

#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="test";


    fd= open("./file1",O_RDWR|O_TRUNC);


    printf("open success:fd=%d\n",fd);


    int n_write= write(fd,buf,strlen(buf));
    if(n_write != -1){
    printf("write %d bytes to file\n",n_write);



    close(fd);
    return 0;

}

二、creat函数使用

函数原型和头文件

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

int creat(const char *pathname, mode_t mode);

  fd= creat("/home//file1",S_IRWXU); //参数1:路径名 参数2:文件类型 

  S_IRWXU可读,可写,可执行

三、编写CP指令

参照Linux命令终端下的cp指令,自主编写一个自己拷贝命令代码,

需要涉及到main函数的参数问题,用到 int main(int argc,char **argv)    //argc 为参数个数 argv为二级指针(字符指针数组)

代码实现

#include <string.h>

#include <stdlib.h>

int main(int argc,char **argv)
{
    int fdSrc;
    int fdDes;
    char *readBuf=NULL;
    if(argc != 3){
      printf("pararm 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);

    fdDes=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
    int n_write=write(fdDes,readBuf,strlen(readBuf));

    close(fdSrc);
    close(fdDes);



    return 0;

}

编译方式

./a.out demo.c new.c

这里argv为demo.c,argv[2]即为new.c

四、修改配置文件

需要用到strstr函数,找到对应要修改的信息位置,并且指针开始指向被查找的字符串开头

int main(int argc,char **argv)
{
    int fdSrc;

    char *readBuf=NULL;
    if(argc != 2){
      printf("pararm 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;

}

编译格式:./a.out TEST.config 

五、写整形数或结构体到文件

写整形数到文件

#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;
    int data=100;
    int data2=0;
    fd= open("./file1",O_RDWR);


    printf("open success:fd=%d\n",fd);


    int n_write= write(fd,&data,sizeof(int));

    lseek(fd,0,SEEK_SET);

    int n_read=read(fd,&data2,sizeof(int));
    printf("read %d\n",data2);
    close(fd);
    return 0;

}

写结构体到文件

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 data[2]={{100,'a'},{101,'b'}};
    struct Test data2;
    fd= open("./file1",O_RDWR);


    printf("open success:fd=%d\n",fd);


    int n_write= write(fd,&data,sizeof(struct Test)*2);

    lseek(fd,0,SEEK_SET);

    int n_read=read(fd,&data2,sizeof(struct Test)*2);
    printf("read %d,%c\n",data[0].a,data[0].c);

    printf("read %d,%c\n",data[1].a,data[1].c);
    close(fd);
    return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值