Linux文件操作(2)

一、文件编程练手(1):实现Linux的cp命令

思路: 打开src.c—>读src到buf–>打开/创建des.c–>将buf写到des.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;


          if(argc!=3){
    
    
                printf("pararm errpr\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;
}

二、文件编程练手(2):配置文件的修改

要修改的配置文件

SOEED=3
LENG=6
SCORE=9
LEVEL=5

将配置文件LENG=6修改为LENG=5

#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("pararm errpr\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;
}

三、文件记录一个结构体

//注意一句话:指针等于地址
1.写一个整数到文件
创建一个空文件file2 touch file2

#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("./file2",O_RDWR);

          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;
}

打开文件file2发现会出现如下情况,并不影响程序对它的写入读写操作。(下同)
在这里插入图片描述
2.写结构体数组到文件

#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[2];
  fd=open("./file2",O_RDWR);

  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",data2[0].a,data2[0].c);
  printf("read %d,%c\n",data2[1].a,data2[1].c);

   close(fd);

   return 0;
}

总结:解决思维误区,不是只有缓冲区是char型才能对文件进行读写操作。

猜你喜欢

转载自blog.csdn.net/qq_46777053/article/details/108681802