zoukankan      html  css  js  c++  java
  • File IO (AsciiToBinary / BinaryToAscii)

    AsciiToBinary

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 int main(int argc,char *argv[])
     5 {
     6     FILE *ascFile = NULL;
     7     FILE *binFile = NULL;
     8     double fnum;
     9     char Buffer[50];
    10     char msg[40];
    11     int inum;
    12     int i;
    13 
    14     ascFile = fopen(argv[1],"r");
    15     if( ascFile == NULL)
    16     {
    17         printf("ascFile is null! \n");
    18         exit(1);
    19     }
    20     
    21     binFile = fopen(argv[2],"wb");
    22     if( binFile == NULL)
    23     {
    24         printf("binFile is null! \n");
    25         exit(1);
    26     }
    27 
    28     for( i = 0; i< 10; i++)
    29     {
    30         fgets(Buffer,50,ascFile);
    31         sscanf(Buffer,"%lf",&fnum);
    32         fwrite(&fnum,sizeof(double),1,binFile);
    33     }
    34 
    35     for( i = 0; i< 10; i++)
    36     {
    37         fgets(msg,40,ascFile);
    38         fwrite(msg,sizeof(char),40,binFile);    
    39     }
    40 
    41 
    42     for( i = 0; i< 10; i++)
    43     {
    44         fgets(Buffer,50, ascFile);
    45         sscanf(Buffer,"%d",&inum);
    46         fwrite(&inum,sizeof(int),1,binFile);
    47     }
    48 
    49     fclose(ascFile);
    50     fclose(binFile);
    51 
    52     return 0;
    53 }

    BinaryToAscii

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
        FILE *binFile = NULL;
        FILE *ascFile = NULL;
        int inum; double fnum; int i;
        char Buffer[40];
    
        binFile = fopen(argv[1],"rb");
        if(binFile == NULL)
        {
            printf("binFile is NULL!\n");
            exit(1);
        }
    
        ascFile = fopen(argv[2],"w");
        if(ascFile == NULL)
        {
            printf("ascFile is NULL!\n");
            exit(1);
        }
    
        
        for (i = 0; i < 10; i++)
        {
            fread(&fnum,sizeof(double),1,binFile);    
            fprintf(ascFile,"%.4f\n",fnum);
        }
            
    
        for(i = 0; i< 10;i++)
        {
            fread(Buffer,sizeof(char),40,binFile);
            fprintf(ascFile,"%s",Buffer);
        }
    
    
        for(i = 0; i< 10; i++)
        {
            fread(&inum,sizeof(int),1,binFile);
            fprintf(ascFile,"%d\n",inum);
        }
        
        fclose(ascFile);
        fclose(binFile);
    
        return 0;
    }
  • 相关阅读:
    oracle 闪回操作--区别于快照
    easyui 低版本下拉多选框绑定onChange事件样式失真问题
    kvm虚拟机网络配置-网桥
    CentOS7.5使用KVM创建虚拟机
    梦醒时分
    姑娘
    Ventoy+WePE 装机教程
    PG-SSL安全配置
    转载-如何做一份完善的补丁分析
    网络流24题部分题解
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/12914087.html
Copyright © 2011-2022 走看看