Linux文件 系统编程(二)

对比vim 代码

退出 :qall  或者 两次:wq

文件操作应用之实现cp指令

实现cp

0           1          2        =  3个参数  argc
cp        src.c      des.c
argv[0]   argv[1]    argv[2]    =  argv[]


1.C语言参数       ./a.out  

2.思路

        a.打开src.c
        b.读src到buf
        c.打开/创建des.c
        d.将buf写入到des.c
        e.close两个文件

参数的实现传输

cptest1.c
#include <stdio.h>

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

        printf("total param:%d\n",argc);
        printf("NO.1 param:%s\n",argv[0]);
        printf("NO.2 param:%s\n",argv[1]);
        printf("NO.2 param:%s\n",argv[2]);

        return 0;
}

运行结果:

cptest2.c
#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 fdDes;
        char *readBuf = NULL; //定义一个buf空间用来存放

        if(argc != 3)
        {
                printf("param error\n");
                exit(-1);
        }


        fdSrc = open(argv[1],O_RDWR);  //1.打开src.c

        int size = lseek(fdSrc,0,SEEK_END); //光标来确定buf所需要的空间
        lseek(fdSrc,0,SEEK_SET);//再将光标返回到初始位置

        readBuf = (char *)malloc(sizeof(char)*size + 8);//给野指针开辟空间

        int n_read = read(fdSrc,readBuf,size);//2.读src到buf


        fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);//3.打开des.c

        int n_write = write(fdDes,readBuf,strlen(readBuf));//4.将buf写入des.c

        //5.关闭两个文件
        close(fdSrc);
        close(fdDes);


        return 0;
}

 文件操作应用之修改程序配置文件

配置文件的修改   (针对字符串)

如:
    SPEED = 5
    LENG  = 100
    SCORE = 90
    LEVEL = 95
       

 初始为这样

最终效果   完成修改LENG的值

configtest3.c
#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;
        char *readBuf = NULL;

        if(argc != 2)
        {
                printf("param 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=");  //1.去查找需要修改的位置

        if(p==NULL){                      //做个测试 要是没找到有个提示
                printf("not found\n");
                exit(-1);
        }
        p = p+strlen("LENG="); //2.去计算要修改的 会变动的数值
        *p = '5'; //3.取内容  去修改数值

        lseek(fdSrc,0,SEEK_SET);//4.将光标跳回最开头  以防修改时候另起一大段 选择直接覆盖
        int n_write = write(fdSrc,readBuf,strlen(readBuf));

        close(fdSrc);


        return 0;

}

针对上述修改文件配置(字符串'5') 去实现写一个整数到文件

inttest4.c
#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 data1 = 100;
        int data2 = 0;


        fd = open("./file1",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;

}

运行结果 

写入结构体

inttestnew5.c
#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\n",data2.a,data2.c);

        close(fd);



        return 0;

运行结果为: 

写入结构体的多个数值

inttestnewagain6.c 
#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[2] = {
   
   {100,'a'},{101,'b'}};
        struct Test data2[2];


        fd = open("./file1",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;

运行结果为: 

猜你喜欢

转载自blog.csdn.net/weixin_46016743/article/details/114895094