c binary file read write file, c language binary (01) is converted into txt format text, c read the file name of the variable

c language binary (01) is converted into txt format text:

 

The following program can only be achieved ascall corresponding character conversion, if Chinese characters appear in the document, an error occurs.

This procedure To create a text file format a1.txt their input, the compiler can convert the character before the text file is 255 bytes within respective AscII code values ​​into binary representation, and saved in the output file a2.txt. And then restore the binary file and stored in b2.txt file.

Reference link: https: //www.jb51.net/article/158695.htm

. 1 #include <cstdio>
 2 #include <stdio.h>
 . 3 #include < String .h>
 . 4  #define nSize. 8
 . 5   
. 6  void print_2 ( int val2);
 . 7  / * ********** text binary file transfer *************************************** * / 
. 8  void Text2Bin ( const  char * SiN, const  char * sOut) {
 . 9    char A [ 255 ];
 10    int COUNT = 0 ;
 . 11    int CH;
 12 is    for (int j=0; j<255;j++)a[j]='\0';
13   FILE* fin=fopen(sIn,"r");
14   FILE* fout=fopen(sOut,"w");
15   for(int i=0 ; i<=255 ; i++)fscanf(fin,"%c",&a[i]);
16   for(int k=0 ; k<=254&&a[k] !='\0'; k++)
17   {  
 18 is      CH = A [K];
 . 19      for ( int A = . 7 ; A> = 0 ; A--) fprintf (FOUT, " % D " , A & CH >> . 1 );
 20 is      // fprintf (FOUT, " \ n-"); 
21 is    }
 22 is    fclose (FIN);
 23 is    fclose (FOUT);
 24  }
 25  / * ********** ********* binary-to-text file * / 
26 is  void Bin2Text ( const  char * SiN, const  char * sOut) {
 27    the FILE * FIN = the fopen (SiN,"r");
28   FILE* fout=fopen(sOut,"w");
29   char str[255*8];
30   for(int r=0; r<255 ;r++) str[r]='\0';
31   int i = 0, j = 0, iTemp = 0, flag = 0;
32   int ibina[NSIZE];     
33   char cRead[NSIZE];       
34   char cChar;
35   for(int a=0 ; a<=255 ; a++)fscanf(fin,"%c",&str[a]);
36   //for(int f=0 ; f<=255 ; f++)printf("%c",str[f]);
37   while(flag <= 255){
38     //printf("%d",flag);
39     for(int b=flag ; b>=flag && b<flag+NSIZE ; b++)
40     {
41       //printf("%d",b%8);
42       cRead[b%8] = str[b];
43       //printf("%c",cRead[b%8]);
44     }
45     for(i = 0; i < NSIZE; i++)
46     {
47       ibina[i] = (cRead[i]-'0'); 
48     }
49     iTemp = 1;
50     cChar = 0;
51     for(j = 7; j >=0 ; j--)
52     {
53       //printf("%c",ibina[j]);
54       //printf("%d\n",cChar);
55       cChar+=ibina[j]*iTemp;
56       iTemp *= 2;
57     }
58     printf("%c",cChar);
59     fprintf(fout,"%c",cChar);
60     flag=flag+8;
61   }
62   fclose(fin);
63   fclose(fout);
64 }
65  
66 int main(){
67   Text2Bin("d:\\a1.txt","d:\\a2.txt"); //文件位置注意一下
68   Bin2Text("d:\\a2.txt","d:\\b2.txt");
69   printf("\nSuccessfully converted file!\n"); 
70   return 0;
71 }

 

c binary file read write to the file:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<windows.h>
 4 #include<time.h>
 5 #define maxn 50
 6 #define NSIZE 8
 7 typedef struct
 8 {
 9     char na1[50],na2[50],na3[50],na4[50],na5[50];
10 } Route;
11 Route m[100005],temp;
12 char filename[50];
13 int number;
14 void add_Route()
15 {
16     system("cls");
17     printf("FlightNumber: OrginAirport: DestinationAirport: DepartureDate: \n");
18     scanf("%s%s%s%s%s",m[number].na1,m[number].na2,m[number].na3,m[number].na4,m[number].na5);
19     number++;
20     system("pause");
21 }
22 void export_file_Route()  //导出文件
23 is  {
 24      int I;
 25      the FILE * Output = the fopen ( " souce.txt " , " WB " ); // generated stud.dat memo file can not be opened with the present display "distortion" open. 
26 is      IF (Output == NULL)
 27      {
 28          the printf ( " Unable to Open File " );
 29          Exit ( 0 );
 30      }
 31 is      fwrite (& m, the sizeof (the Route), Number, Output);
 32      fclose (Output);
 33 is  }
 34 is  void import_file_Route() //导入文件
35 {
36     int i;
37     char ch;
38     number=0;
39     FILE * input = fopen("souce.txt","rb");
40     if(input == NULL)
41     {
42         printf("Unable to open file");
43         exit(0);
44     }
45     int ans;
46     while((ans = fread(m,sizeof(Route),10005/*最多读取5个数据项*/,input))!=0)
47     {
48         for(i=0; i<ans; ++i)
49             printf("%s %s %s %s %s\n",m[i].na1,m[i].na2,m[i].na3,m[i].na4,m[i].na5),number++;
50     }
51     fclose(input);
52 }
53 void show()
54 {
55     int i;
56     system("cls");
57     printf("FlightNumber: OriginAirport: DestinationAirport: DepartureDate: \n");
58     for(i=0; i<number; ++i)
59     {
60         printf("%s %s %s %s %s\n",m[i].na1,m[i].na2,m[i].na3,m[i].na4,m[i].na5);
61     }
62     system("pause");
63 }
64 int main()
65 {
66     import_file_Route();
67     add_Route();
68     export_file_Route();
69     import_file_Route();
70     show();
71 }

 

 

c read the file name variable:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<windows.h>
 4 #include<time.h>
 5 #define maxn 50
 6 #define NSIZE 8
 7 char filename[50];
 8 int main()
 9 {
10     FILE *fp=NULL;
11     printf("输入文件名:\n");
12     scanf("%s",filename);
13     strcat(filename,".txt");
14     fp=fopen(filename,"w");
15     fprintf(fp,"Success!\n");
16     fclose(fp);
17     return 0;
18 }

 

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11965604.html