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 }
原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/11965604.html
时间: 2024-10-08 08:16:30