Linux---文件---基本操作练习



学习目标:

1文件操作,修改程序的配置文件.

2.写一个 整数 或者 结构体 到文件

3.实现Linux CP指令的代码(a.c语言man函数的参数 b.)




学习内容:

一、修改程序的配置文件

例如修改 SPEED=5;

思路:首先 找到SPEED=的位置---定位5的位置---修改

strstr 函数的使用:返回值为 char *型  char*a = strstr(文件描述符,“需要查找的字符串”);

#include<stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
       int fd;    
       char *readbuf;
        
       fd = open("./peizhi",O_RDWR); 
       int size = sleek(fd,0,SEEK_END);//用lseek计算文件长度
       sleek(fd,0,SEEK_SET);//计算完大小要把光标恢复首位置,否则strstr时,no find
       
       readbuf = (char *)malloc(sizeof(char)*size + 8);           
       int n_read = read(fd,readbuf,size);
       char *p = strstr(readbuf,"SPEED=");
       if(p == NULL){
            printf("no find\n");
            exit(-1);       
       }    
       p = p + strlen("SOEED=");
       *p = '6';
       int n_write = write(fd,readbuf,strlen(readbuf));
        
       return 0;
}

二、写一个 整数 或者 结构体 到文件

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int fd;    
    int date1 = 6;  
    int date2 = 8;
    
    fd = open(“./file1”,O_RDWR);
    int n_write = write(fd,&date1,sizeof(int));
    
    int n_read = read(fd,&date2,sizeof(int));
    printf("read = %d\n",date2);
    

    return 0;
}

三、实现Linux CP指令的代码

1、c语言man 函数的参数

main 函数的标准原型

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

2、基本思考与思路:

        a.例如cp指令的用法,cp file1.c file2.c   本质上就是利用man函数的参数(c语言的参数)

我们可以写一个自己的cp函数  来实现将文件内容的复制

        b.基本思路:打开目标文件---将目标文件内容读取到buf里---打开或创建新文件----将buf里内容写入到新文件里---close

3、代码

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

int main(int argc,char **argv){//C语言参数将文件与数组联系起来

        int fdgc;
        int fdgv;
        char *buf = NULL;

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

        fdgc = open(argv[1],O_RDWR);   //①打开
        int size = lseek(fdgc,0,SEEK_END);
        lseek(fdgc,0,SEEK_SET);    //利用lseek计算大小,计算完需要将光标移至首

        buf =(char *) malloc(sizeof(char)*size + 8);

        int n_read = read(fdgc,buf,1024);//②将目标文件内容读取到buf

        fdgv = open(argv[2],O_RDWR|O_CREAT,0600);

        int n_write = write(fdgv,buf,strlen(buf));//③将buf内容写入到新文件

        close(fdgc);
        close(fdgv);

        return 0;
}
                                                              37,1          Bot

优化:a.  read的大小1024太小,将其改为size

           b. 写入新文件内容时,存在   新文件存在并且原始里面有内容  情况,因此需要用到open里的:O_TRUNC        如果文件存在,并且以只写/读写方式打开,则清空文件全部内容

       fdgv = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);就解决了问题


学习产出:

1.利用sleek(fd,0,SEEK_END); 计算文件大小、sleek(fd,0,SEELK_SET);定位光标首位置

   open计算文件描述符的函数open("./text.c",O_RDWR);打开文件要“./“

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

 write()会把参数buf所指的内存写入count个字节到参数放到所指的文件内。

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

read()会把参数fd所指的文件传送count 个字节到buf 指针所指的内存中。

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值