File IO (AsciiToBinary / BinaryToAscii)

Posted jasperzhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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! 
");
18         exit(1);
19     }
20     
21     binFile = fopen(argv[2],"wb");
22     if( binFile == NULL)
23     {
24         printf("binFile is null! 
");
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!
");
        exit(1);
    }

    ascFile = fopen(argv[2],"w");
    if(ascFile == NULL)
    {
        printf("ascFile is NULL!
");
        exit(1);
    }

    
    for (i = 0; i < 10; i++)
    {
        fread(&fnum,sizeof(double),1,binFile);    
        fprintf(ascFile,"%.4f
",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
",inum);
    }
    
    fclose(ascFile);
    fclose(binFile);

    return 0;
}

 

以上是关于File IO (AsciiToBinary / BinaryToAscii)的主要内容,如果未能解决你的问题,请参考以下文章

IO:File类(java.io.File)

System.IO.File.Delete() / System.IO.File.Move() 有时不起作用

IO_FILE fopen

java io包File类

java.io.File类

android java.io.File.fixSlashes(File.java:185)