Linux 库函数文件操作

来源:互联网 发布:php 九宫格抽奖源代码 编辑:程序博客网 时间:2024/06/06 20:50


利用库函数操作文件具有跨平台的作用(库函数不随系统平台而变,即不管win还是Linux都适用),而利用Linux 系统调用操作文件,需要考虑平台的兼容性。

库函数 - 读文件

size_t fread(void *ptr, size_t size, size_t n, FILE *stream)

功能:stream指向的文件中读取n个字段,每个字段为size字节,并将读取的数据放入ptr所指向的字符数组中,返回实际已读取的字节数。(读出来的数据量为size*n

 

库函数 - 写文件

size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream)

功能:从缓冲区ptr所指向的数组中把n个字段写到stream指向的文件中,每个字段长为size个字节,返回实际写入的字段数。

 

库函数 - 创建和打开

FILE *fopen(const char *filename, const char *mode)

filename打开的文件名(包含路径,缺省为当前路径)

mode打开模式 

实例代码

[plain] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. root@wl-MS-7673:/home/wl/桌面/c++# cat -n file_lib_copy.cpp   
  2.      1    
  3.      2  #include <stdio.h>  
  4.      3  #include <string.h>  
  5.      4  #include <stdlib.h>  
  6.      5  #define BUFFER_SIZE 1024  
  7.      6    
  8.      7  /*  
  9.      8   * 程序入口  
  10.      9   * */  
  11.     10  int main(int argc,char **argv)  
  12.     11  {  
  13.     12      FILE *from_fd;  
  14.     13      FILE *to_fd;  
  15.     14      long file_len=0;  
  16.     15      char buffer[BUFFER_SIZE];  
  17.     16      char *ptr;  
  18.     17    
  19.     18  /*判断入参*/  
  20.     19  if(argc!=3)  
  21.     20  {  
  22.     21      printf("Usage:%s fromfile tofile\n",argv[0]);  
  23.     22      exit(1);  
  24.     23  }   
  25.     24    
  26.     25  /* 打开源文件 */   
  27.     26  if((from_fd=fopen(argv[1],"rb"))==NULL)   
  28.     27  {   
  29.     28      printf("Open %s Error\n",argv[1]);   
  30.     29      exit(1);   
  31.     30  }   
  32.     31    
  33.     32  /* 创建目的文件 */   
  34.     33  if((to_fd=fopen(argv[2],"wb"))==NULL)   
  35.     34  {   
  36.     35      printf("Open %s Error\n",argv[2]);   
  37.     36      exit(1);   
  38.     37  }   
  39.     38    
  40.     39  /*测得文件大小*/  
  41.     40  fseek(from_fd,0L,SEEK_END);  
  42.     41  file_len=ftell(from_fd);  
  43.     42  fseek(from_fd,0L,SEEK_SET);  
  44.     43  printf("form file size is=%d\n",file_len);  
  45.     44    
  46.     45  /*进行文件拷贝*/  
  47.     46  while(!feof(from_fd))   
  48.     47  {   
  49.     48      fread(buffer,BUFFER_SIZE,1,from_fd);  
  50.     49      if(BUFFER_SIZE>=file_len)  
  51.     50      {  
  52.     51          fwrite(buffer,file_len,1,to_fd);  
  53.     52      }  
  54.     53      else   
  55.     54      {  
  56.     55          fwrite(buffer,BUFFER_SIZE,1,to_fd);  
  57.     56          file_len=file_len-BUFFER_SIZE;  
  58.     57      }  
  59.     58      bzero(buffer,BUFFER_SIZE);  
  60.     59  }   
  61.     60  fclose(from_fd);   
  62.     61  fclose(to_fd);   
  63.     62  exit(0);   
  64.     63  }   
  65.     64    
  66.     65    
  67. root@wl-MS-7673:/home/wl/桌面/c++# g++ file_lib_copy.cpp -o file_lib_copy  
  68. file_lib_copy.cpp: 在函数‘int main(int, char**)’中:  
  69. file_lib_copy.cpp:43:41: 警告: 格式 ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat]  
  70. root@wl-MS-7673:/home/wl/桌面/c++# ./file_lib_copy file_lib_copy.cpp test2.c  
  71. form file size is=1030  
  72. root@wl-MS-7673:/home/wl/桌面/c++#


0 0
原创粉丝点击