zoukankan      html  css  js  c++  java
  • 数据文件——之将结构写入二进制文件

    代码:

     1 //This is c program code!
     2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
     3   * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_12.c
     4   * 版权声明: *** :(魎魍魅魑)MIT
     5   * 联络信箱: *** :guochaoxxl@163.com
     6   * 创建时间: *** :2020年11月21日的下午04:25
     7   * 文档用途: *** :数据结构与算法分析-c语言描述
     8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
     9   * 修订时间: *** *2020年第46周 11月21日 星期六 下午05:03 (326天)
    10   * 文件描述: *** :自行添加
    11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
    12 #include <stdio.h>
    13 
    14 typedef struct _bio{
    15     char name[15];
    16     int rollNo;
    17     int age;
    18     float weight;
    19 } Bio;
    20     
    21 void closeState(int clo, FILE *fPtr){
    22     clo = fclose(fPtr);
    23     if(clo == -1){
    24         puts("File-closing failed!");
    25     }
    26     if(clo == 0){
    27         puts("File is closed successfully.");
    28     }
    29 
    30     return;
    31 }   
    32     
    33 int main(int argc, char **argv)
    34 {   
    35     char flag = 'y';
    36     FILE *fPtr = fopen("agentsb.dat", "wb");
    37     if(fPtr != NULL){
    38         printf("File agentsb.dat is opened successfully.
    ");
    39         Bio bio;
    40         while(flag == 'y'){
    41             printf("Enter name, rooNo, age and weight of agent: ");
    42             scanf("%s %d %d %f", bio.name, &bio.rollNo, &bio.age, &bio.weight);
    43             fwrite(&bio, sizeof(bio), 1, fPtr);
    44             int tmp = getchar();
    45             //fflush(stdin);
    46             printf("Any more records(y/n): ");
    47             scanf("%c", &flag);
    48         }
    49         int clo = fclose(fPtr);
    50         closeState(clo, fPtr);
    51     }else{
    52         puts("File-open failed!");
    53     }
    54 
    55     return 0;
    56 }
  • 相关阅读:
    jchdl
    jchdl
    UVa 10256 (判断两个凸包相离) The Great Divide
    UVa 11168 (凸包+点到直线距离) Airport
    LA 2572 (求可见圆盘的数量) Kanazawa
    UVa 10652 (简单凸包) Board Wrapping
    UVa 12304 (6个二维几何问题合集) 2D Geometry 110 in 1!
    UVa 10674 (求两圆公切线) Tangents
    UVa 11796 Dog Distance
    LA 3263 (平面图的欧拉定理) That Nice Euler Circuit
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/14054185.html
Copyright © 2011-2022 走看看