zoukankan      html  css  js  c++  java
  • 文件保存读取

    FILE * out_fp;
     const char * out_file = "output.bin";
     out_fp = fopen(out_file, "wb" );
     if( out_fp == NULL ){
         printf(" file open failed
    ");
         exit(1);
     }
    size_t read_size;
    size_t reality_size;
    if(reality_size!= (read_size = fwrite(model, sizeof(T), read_size, out_fp))){
    }
    
    
    

      

    http://blog.csdn.net/liangxanhai/article/details/8026496
    fscanf函数详解:
    fscanf()函数(有点像正则表达式):
    
    功 能: 从一个流中执行格式化输入,fscanf遇到空格和换行时结束,注意空格时也结束。
    用 法:int fscanf(FILE *stream, char *format,[argument...]);
    
    file.txt 内容为:
    10 10
    16 2
    以下为读取内部文件内容方法:
    char *FilePath = "test/file.txt";
    char Table[1024];
    sprintf(Table, "%s", FilePath);
    
    FILE* file = fopen(Table, "r");
    if (!fscanf(file, "%d %d", &num_bin, &colorspace)) {
        printf("Error in Reading txt: %s %d %s
    ", __FILE__, __LINE__, Table);
    }
    
    又如:
    首先我有一个data.txt的文件里面的数据格式如下:
    2,50,41,w,20.585828
    4,52,51,r,52.012547
    .........................
    
     许多条类似的记录,都是以""来分隔的
     我实现的功能就是把上面文件中的数据的五个字段赋值给相应的五个变量,并且输出这些变量的值
     #include<stdio.h>
    #include<stdlib.h>
    
    
    int main()
    {
      int fd;
      long dev;
      long offset;
      long length;
      char ch;
      double ts=0.000000;
      if((fd=fopen("/home/haixian/ceshi/data.txt","r"))<0)
       {
         printf("open the file is error!
    ");
         return -1;
       }
      fseek(fd,0,SEEK_SET);
      
      while(5==fscanf(fd,"%ld,%ld,%ld,%c,%lf
    ",&dev,&offset,&length,&ch,&ts))
      {        //在这里就是第二个参数指定分隔参数的格式,在这里使用的是,来分隔。
            //这样就很容易的获取了记录的各个字段的值并不需要自己编写函数来进行解析什么的。
         printf("%ld,%ld,%ld,%c,%lf
    ",dev,offset,length,ch,ts);
         return -1;
      }
        close(fd);
        return 0;
    }
     
  • 相关阅读:
    Hard Rock
    Codeforces Round #416 (Div. 2) B. Vladik and Complicated Book
    codeforces 793B. Igor and his way to work
    codeforces 1B Spreadsheets
    HDU 1069 Monkey and Banana
    codeforces 2B The least round way
    【机器学习】 通俗说拟合
    python-八皇后问题
    python-核心知识思维导图
    python-@property 属性
  • 原文地址:https://www.cnblogs.com/hansjorn/p/5108715.html
Copyright © 2011-2022 走看看