c文件二進制讀取寫入文件、c語言實現二進制(01)轉化成txt格式文本、c讀取文件名可變


c語言實現二進制(01)轉化成txt格式文本:

 

下面的程序只能實現ascall對應字符轉換,如果文件內出現中文字符,則會出現錯誤。

本程序要自己創建個文本格式的輸入文件a1.txt,編譯后能將文本文件前255字節以內的字符轉換成相應的AscII碼值的二進制表示,並存入輸出文件a2.txt中。然后再將二進制文件還原並存入b2.txt文件。

參考鏈接: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 /***********文本文件轉二進制**********/
 8 void Text2Bin(const char* sIn,const char* sOut){
 9   char a[255];
10   int count = 0;
11   int ch;
12   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     ch = a[k];
19     for(int a=7;a>=0;a--) fprintf(fout,"%d",ch>>a&1);
20     //fprintf(fout,"\n");
21   }
22   fclose(fin);
23   fclose(fout);
24 }
25 /***********二進制文件轉文本文件**********/
26 void Bin2Text(const char* sIn,const char* sOut){
27   FILE* fin=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文件二進制讀取寫入文件:

 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 {
24     int i;
25     FILE * output  = fopen("souce.txt","wb");//生成的stud.dat文件無法用記事本打開,打開后顯示“亂碼”。
26     if(output == NULL)
27     {
28         printf("Unable to open file");
29         exit(0);
30     }
31     fwrite(&m,sizeof(Route),number,output);
32     fclose(output);
33 }
34 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讀取文件名可變:

 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 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM