文件编程练习

自己实现linux CP指令

实现cp指令的思路:

  • 打开要复制的原文件
  • 读原文件的内容到buf
  • 打开或者创建要粘贴的文件
  • 将buf里面的内容写到目标文件
  • 关闭两个文件

main 函数的标准原型:

main 函数的标准原型应该是 int main(int argc, char *argv[]);argc 是命令行参数的个数。而 argv 是一个指向指针的指针,为什么不是指针数组呢?因为前面讲过,函数原型中的[]表示指针而不表示数组,等价于 char **argv 。那为什么要写成 char *argv[] 而不写成 char **argv 呢?这样写给读代码的人提供了有用信息,argv 不是指向单个指针,而是指向一个指针数组的首元素。数组中每个元素都是 char * 指针,指向一个命令行参数字符串。

demo:

#include<stdio.h>
int main(int argc,char *argv[])//argc表示参数的个数,argv表示字符串数组是二级指>针
{
        printf("参数总个数是:%d\n",argc);
        printf("第一个参数是:%s\n",argv[0]);
        printf("第二个参数是:%s\n",argv[1]);
        printf("第三个参数是:%s\n",argv[2]);
        return 0;
}

程序运行的结果:
fhn@ubuntu:~/linuxfile$ ./cpfile src des
参数总个数是:3
第一个参数是:./cpfile
第二个参数是:src
第三个参数是:des

实现cp指令:

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h>
int main(int argc,char *argv[])//argc表示参数的个数,argv表示字符串数组是二级指针
{
        int fd;
        int fd2;
        int size;
        int n_read;
        int n_write;
        char*readbuf=NULL;
        if(argc!=3){
                printf("输入参数个数有误\n");
                exit(0);//正常退出              
        }
        fd=open(argv[1],O_RDONLY);
        if(fd==-1){
                printf("文件打开错误\n");
                perror("open");
                exit(0);
        }
        size=lseek(fd,0,SEEK_END);
        readbuf=(char*)malloc(sizeof(char)*size+8);
        lseek(fd,0,SEEK_SET);
        n_read=read(fd,readbuf,sizeof(char)*size+8);
        if(n_read==-1){
                printf("文件读取错误\n");
                perror("read");
                exit(0);
        }
        fd2=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
        if(fd2==-1){
                printf("目标文件打开失败\n");
                perror("open");
                exit(0);
        }
        n_write=write(fd2,readbuf,strlen(readbuf));
        if(n_write==-1){
                printf("文件写入失败");
                perror("write");
                exit(0);
        }
        close(fd);
        close(fd2);
        return 0;
}

配置文件的修改:

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h>
int main(int argc,char *argv[])//argc表示参数的个数,argv表示字符串数组是二级指针
{
        int fd;
        int size;
        int n_read;
        int n_write;
        char* find=NULL;
        char* readbuf=NULL;
        if(argc!=2){
                printf("输入参数个数有误\n");
                exit(0);//正常退出              
        }
        fd=open(argv[1],O_RDWR);
        if(fd==-1){
                printf("文件打开错误\n");
                perror("open");
                exit(0);
        }
        size=lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
        readbuf=(char*)malloc(sizeof(char)*size);
        n_read=read(fd,readbuf,size*sizeof(char));
        if(n_read==-1){
                printf("文件读取错误\n");
                close(fd);
                perror("read");
                exit(0);
        }
        find=strstr(readbuf,"heigh=");
        if(find==NULL){
                printf("配置文件中没有要修改的内容\n");
                close(fd);
                exit(0);
        }
        find=find+strlen("heigh=");
        *find='1';
        *(++find)='8';
        *(++find)='0';
        lseek(fd,0,SEEK_SET);
        n_write=write(fd,readbuf,strlen(readbuf));
        if(n_write==-1){
                printf("写入失败\n");
                close(fd);
                perror("write");
                exit(0);
        }
        close(fd);
        return 0;
}

写一个整数到文件:

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

int main()
{
        int fd;
        int a=110;
        int b=0;
        fd=open("./test.config",O_RDWR|O_APPEND);
        write(fd,&a,sizeof(int));
        lseek(fd,-4,SEEK_END);
        //因为写入的是整型,所以光标要移动4个字节
        read(fd,&b,sizeof(int));
        printf("写入的是:%d\n",b);
        close(fd);
        return 0;
}



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

int main()
{
        int fd;
        short int a=110;
        int b=0;
        fd=open("./test.config",O_RDWR|O_APPEND);
        write(fd,&a,sizeof(short int));
        lseek(fd,-2,SEEK_END);
        read(fd,&b,sizeof(int));
        printf("写入的是:%d\n",b);
        close(fd);
        return 0;
}

写结构体到文件:

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct Test
{
        int a;
        char b;
};
int main()
{
        int fd;
        struct Test Data1={1,'a'};
        struct Test Data2;
        fd=open("./test.config",O_RDWR|O_APPEND);
        write(fd,&Data1,sizeof(struct Test));
        lseek(fd,-8,SEEK_END);
        read(fd,&Data2,sizeof(struct Test));
        printf("结构体大小是:%d\n",(int)sizeof(struct Test));
        printf("写入的是a=%d,b=%c\n",Data2.a,Data2.b);
        close(fd);
        return 0;
}

写结构体数组到文件:

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct Test
{
        int a;
        char b;
};
int main()
{
        int fd;
        struct Test Data1[2]={{1,'a'},{2,'b'}};
        struct Test Data2[2];
        fd=open("./test.config",O_RDWR|O_APPEND);
        write(fd,&Data1,sizeof(struct Test)*2);
        lseek(fd,-16,SEEK_END);
        read(fd,&Data2,sizeof(struct Test)*2);
        printf("写入的是a=%d,b=%c\n",Data2[0].a,Data2[0].b);
        printf("写入的是a=%d,b=%c\n",Data2[1].a,Data2[1].b);
        close(fd);
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值