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 }

原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/11965604.html

时间: 2024-08-01 13:50:36

c文件二进制读取写入文件、c语言实现二进制(01)转化成txt格式文本、c读取文件名可变的相关文章

以二进制方式写入文件,并测试读取

以?BinaryFormatter 方式写入,并读取 其中牵涉到"序列化对象",等找到完整数据再补上 原文:大专栏  以二进制方式写入文件,并测试读取 原文地址:https://www.cnblogs.com/chinatrump/p/11458338.html

读取/写入文件

读取文件: #直接读取 for line in open("d:\serverlist.txt"): print(line) #使用readline()方法读取 file = open('d:\serverlist.txt')line = file.readline()while line:print(line,end=‘’)file.close() #使用read()方法读取 f = open("d:\out.txt",'r')mm=f.read()f.close

pandas-19 DataFrame读取写入文件的方法

pandas-19 DataFrame读取写入文件的方法 DataFrame有非常丰富的IO方法,比如DataFrame读写csv文件excel文件等等,操作很简单.下面在代码中标记出来一些常用的读写操作方法,需要的时候查询一下该方法就可以了. df1.to_csv('df1.csv') # 默认会把 index 也当成一列写入到文件中 df1.to_csv('df2.csv', index=False) # 如果不想显示索引,可以添加第二个参数 index = False df1.to_jso

python读取写入文件方法SringIO,BytesIO

python中不仅仅可以在磁盘中写入文件,还允许直接在内存中直接写入数据:需要借助StringIO和BytesIO来实现: 1.直接操作StringIO from io import StringIO #载入对象 f=StringIO() #创建变量指向对象 f.write('hello,') #写入数据 f.write(' ') f.write('world.') print(f.getvalue()) #依次打印获得的数据 getvalue()的方法用于获取写入的str 2.初始化Strin

Node.js——fs模块(文件系统),创建、删除目录(文件),读取写入文件流

1 /* 2 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 3 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 4 3. fs.writeFile 写入文件(文件不存在就创建,但不能创建目录) 5 4. fs.appendFile 写入追加文件 6 5. fs.readFile 读取文件 7 6. fs.readdir 读取目录 8 7. fs.rename 重命名 9 8. fs.rmdir 删除目录 10 9. fs.unlink 删除文件 11 */ 12

C#文本写入文件,追加写入文件

写入文件和这个对象 StreamWriter 1 using (StreamWriter fs = new StreamWriter(path, true)) 2 { 3 fs.WriteLine(strLog); 4 } 这个看到那个蓝色的true了没,个就是追加的标记,如果不写的话,那么你所有写一次,之前的都会被覆盖掉.

逐行读取文件,逐行写入文件

java的io流相关的类实在是“太丰富”了,搞得有选择困难症的人有点无从下手.当然本菜鸟对java的io也是了解的不是很清楚,习惯用InputStrem,OutSteam的相关子类来处理二进制流如图片,用Reader,Writer相关的子类处理字符流如文本文件.为了提高效率使用了缓冲机制等.感觉缓冲机制多有于字符流,不知道二进制有那些缓冲机制,真的不了解得google一下.感觉博客园上的文章整体比csdn的文章的水平要高.而且看到博客园上的文章都质量很高,相比之下本菜鸟写的就会想起那句话:Too

Java向自定义文件夹中写入文件

File file = new File("E://rawSpeechRecordData//"); File newFile = null; if (!file.exists()) { file.mkdir(); if (file.isDirectory()) { SimpleDateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss"); String date = format.format(new Da

Python 以txt格式保存和读取json数据

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-15 @author: guaguastd @name: json_with_text.py ''' if __name__ == '__main__': # import json import json # import search from search import twitter_search # import format from forma