zoukankan      html  css  js  c++  java
  • LINUX C 文件读写范例

    代码
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <sys/types.h>
     4 #include <sys/stat.h>
     5 
     6 #include <sys/fcntl.h>   //file
     7 
     8 #define PCFILE  "/root/wencal"
     9 #define PCBACKFILE "/root/wencal_back"
    10 
    11 int main(void)
    12 {
    13     int ret;
    14     int size;
    15     
    16     FILE * Ppointercal;
    17     FILE * Ppointercal_back;
    18     void * filebuffer;
    19 
    20     int pointercal_fd ;
    21 
    22     char modeR='r';
    23     char modeW='w';
    24 
    25     struct stat st;
    26 
    27     pointercal_fd = open(PCFILE, O_RDONLY);
    28     if (pointercal_fd < 0) {
    29         printf("Input file open error\n");
    30         return -1;
    31     }
    32     fstat(pointercal_fd,&st);
    33     size = st.st_size;
    34     close(pointercal_fd);
    35 
    36     filebuffer=(void *)malloc(size);
    37 
    38     Ppointercal = fopen(PCFILE,&modeR);
    39     Ppointercal_back = fopen(PCBACKFILE,&modeW);
    40     ret=fread(filebuffer,1,size,Ppointercal); //1 表示单位为1个字节
    41 
    42     if(ret == size)
    43     {
    44         printf("read success :%d \n",size);
    45         fwrite(filebuffer,1,size,Ppointercal_back);
    46     }
    47     else 
    48     {
    49         printf("read err :  %d\n",size);    
    50     }
    51     
    52     free(filebuffer);
    53     fclose(Ppointercal);
    54     fclose(Ppointercal_back);
    55     return 0;
    56 }
  • 相关阅读:
    Hive中频繁报警的问题
    Hadoop中Namenode的HA查询和切换
    昨天面试遇到的一道C语言题
    【转】MapReduce的优化
    关于linux修改max user processes limits的问题
    Hadoop-2.6.0安装文档
    C#使用RabbitMQ
    windows配置Erlang环境
    【转】linux查看及修改文件权限以及相关
    (转)C#图解—PictureBox.SizeMode 属性
  • 原文地址:https://www.cnblogs.com/leaven/p/1733181.html
Copyright © 2011-2022 走看看