Linux 1.文件编程(main函数的带参的形式、实现cp指令、修改文件里面的数据、对文件写入一个整数、对文件写入一个数组、对文件写入一个结构体、对文件写入结构体数组)

文件编程(main函数的带参的形式、实现cp指令、修改文件里面的数据、对文件写入一个整数、对文件写入一个结构体、写入结构体数组)

main()函数的带参的形式

int main(int argc,char** argv)
{
	printf("total param:%d\n",argc);
	
	printf("1:%s\n",argv[0]);
	printf("2:%s\n",argv[1]);
	printf("3:%s\n",argv[2]);
	
	return 0;
}

运行结果:

在这里插入图片描述

实现cp指令

主体思路

  1. open 打开源文件
  2. 将原文件内容读 read 到 readBuf 中
  3. open 打开创建清零目标文件
  4. 将 readBuf 中的内容写入 write 目标文件
  5. close 关闭两个文件

代码

#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc,char** argv)
{
        if(argc!=3)
        {
                printf("The parram is not good!\n");
                exit(-1);
        }
		1
        char* readBuf=NULL;
		
        int 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);
		
        int fdDes=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
		
        write(fdDes,readBuf,strlen(readBuf));
		
        close(fdSrc);
        close(fdDes);
		
        return 0;
}

修改文件里面的数据

思路

1.打开文件
2.找到要修改关键词的位置
3.光标往后挪到要修改的位置(也可以直接找到修改的位置)
4.修改

比如我的文件里面有

SPEED=2
LENGTH=8
LEVEL=6
DD=11

我想修改LENGTH的内容 让LENGTH=8。

  1. 打开文件

  2. 找到LENGTH=

  3. 让光标(指针)再往后移动到=后面。

  4. 修改数据。(让光标回到文件头)

  5. 写进文件

  6. 关闭文件

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc,char** argv)
{
        if(argc!=2)
        {
                printf("The param is not good!\n");
                exit(-1);
        }

        int fdSrc;
        char* readBuf=NULL;

        fdSrc=open(argv[1],O_RDWR);

        int size=lseek(fdSrc,0,SEEK_END);
        printf("size=%d\n",size);
        lseek(fdSrc,0,SEEK_SET);

        readBuf=(char*)malloc(sizeof(char)*size+8);
        memset(readBuf,0,sizeof(readBuf));

        int n_read=read(fdSrc,readBuf,size);
        printf("----------------------n_read=%d-------------------------\n",n_read);
        char* p=strstr(readBuf,"LENGTH=");

        if(p==NULL)
        {
                printf("not found\n");
                exit(-1);
        }

        p=p+strlen("LENGTH=");
        *p='8';

        lseek(fdSrc,0,SEEK_SET);

        write(fdSrc,readBuf,strlen(readBuf));

        close(fdSrc);

        return 0;
}

原配置文件

在这里插入图片描述

修改后的配置文件

在这里插入图片描述

对文件写入一个整数

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
        int fd=0;
        int data1=8;
        int data2;

        fd=open("./file",O_RDWR);

        int n_write=write(fd,&data1,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 <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
        int fd=0;
        int data1[2]={8,9};
        int data2[2];

        fd=open("./file",O_RDWR);

        int n_write=write(fd,&data1,sizeof(int)*2);

        lseek(fd,0,SEEK_SET);

        int n_read=read(fd,&data2,sizeof(int)*2);
        printf("read %d %d\n",data2[0],data2[1]);

        close(fd);
        return 0;
}

对文件写入一个结构体

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

struct Test
{
        int a;
        char c;
};

int main()
{
        struct Test data1={100,'a'};
        struct Test data2;
        int fd;

        fd=open("./file",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\n",data2.a,data2.c);

        close(fd);
        return 0;
}

对文件写入结构体数组

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

struct Test
{
        int a;
        char c;
};

int main()
{
        int fd;
        struct Test data1[2]={{100,'a'},{99,'b'}};
        struct Test data2[2];

        fd=open("./file",O_RDWR);

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

        lseek(fd,0,SEEK_SET);

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

        close(fd);
        return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值