linux文件编程

cp指令的操作应用

#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 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);
    

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

写一个整数到文件中

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


int main()
{  
   int fd;
   int data1=100;
   int data2=0;
    
   fd=open("./file1",O_RDWR);
   if(fd<0){
      printf("NO file1\n");
   }
  // ssize_t write(int fd, const void *buf, size_t count);
  int n_write=write(fd,&data1,sizeof(int));
 
  lseek(fd,0,SEEK_SET);
  //ssize_t read(int fd, void *buf, size_t count);
  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 <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>

struct Test
{
   int a;
   char c;
};

int main()
{  
   int fd;
   struct Test data1={100,'a'};
   struct Test data2;
    
   fd=open("./file1",O_RDWR);
   if(fd<0){
      printf("NO file1\n");
   }
  // ssize_t write(int fd, const void *buf, size_t count);
  int n_write=write(fd,&data1,sizeof(struct Test));
 
  lseek(fd,0,SEEK_SET);
  //ssize_t read(int fd, void *buf, size_t count);
  int n_read=read(fd,&data2,sizeof(struct Test));

  printf("read:%d\n",data2);

  close(fd);  
   
  return 0;
}
修改程序的配置文件

整数一定要带’‘;

#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 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 *strstr(const char *haystack, const char *needle);
    char *p=strstr(readbuf,"LENG=");
    if(p==NULL){
        printf("pararm error\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;
}
写一个结构体数组到文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>

struct Test
{
  int a;
  char c;
};


int main()
{  
   int fd;
   struct Test data1[2]={{100,'a'},{101,'c'}};
   struct Test data2[2];
    
   fd=open("./file1",O_RDWR);
   if(fd<0){
      printf("NO file1\n");
   }
  // ssize_t write(int fd, const void *buf, size_t count);
  int n_write=write(fd,&data1,sizeof(struct Test)*2);
 
  lseek(fd,0,SEEK_SET);
  //ssize_t read(int fd, void *buf, size_t count);
  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;
}
标准c库打开创建文件读写文件

标准c库的API
fopen

FILE *fopen(const char *path, const char *mode)
*path是文件的路径
*mode是权限
权限有很多,主要是"w+",后续的可以自己百度查

fwrite

  size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
   ptr  buf//缓冲区内存
   size sizeof(char)//大小
   geshu//个数
   which file1//哪个文件

fread

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
和fwrite基本一样
#include<stdio.h>
#include<string.h>
#include <stdlib.h>


int main()
{
     // FILE *fopen(const char *path, const char *mode);
      FILE *fp;
      char *buf="yuexin10kzaidengwohaohaoxue";
      char *readbuf=NULL;  

      fp=fopen("./test.c","w+");

       //size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
      //ptr  buf
     //size sizeof(char)
     //geshu
    //which file1
    fwrite(buf,sizeof(char),strlen(buf),fp);
  

 
   // int fseek(FILE *stream, long offset, int whence);
   fseek(fp,0,SEEK_SET);

   readbuf=(char*)malloc(sizeof(char)*100);
   //size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
   fread(readbuf,sizeof(char),strlen(buf),fp);

   printf("read data:%s\n",readbuf);
    
   fclose(fp);

   return 0;
}

标准c库写进结构体

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

struct Test
{
  int a;
  char c;
};


int main()
{  
   FILE *fp;
   struct Test data1={100,'a'};
   struct Test data2;
    
   fp=fopen("./file1","w+");

  int n_write=fwrite(&data1,sizeof(struct Test),1,fp);
 
  fseek(fp,0,SEEK_SET);

  int n_read=fread(&data2,sizeof(struct Test),1,fp);

  printf("read:%d,%c\n",data2.a,data2.c);

  fclose(fp);  
   
  return 0;
}

标准c库其他函数的补充

#include<stdio.h>
fputc:把字符写进文件

int main()
{
    FILE *fp;
    fp=fopen("./text.c","w+");
    // int fputc(int c, FILE *stream);
    fputc('a',fp);
    fclose(fp);
    return 0;
}

写进多个字符

#include<stdio.h>
#include<string.h>

int main()
{
    FILE *fp;
    int i;
    char *str="xiedaimashiwokuaile";
    int len=strlen(str);
    fp=fopen("./text.c","w+");
    for(i=0;i<len;i++){   
        fputc(*str,fp);
        str++;
    }
    fclose(fp);
    return 0;
}

feof:当文件没有到尾巴是0,到尾巴就是非0
fgetc:获取fp文件中的内容

#include<stdio.h>
#include<string.h>

int main()
{
    FILE *fp;
    int i;
    char c;
    fp=fopen("./text.c","r");
    while(!feof(fp)){
          c=fgetc(fp);
          printf("%c",c);
    }
    fclose(fp);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值