C++/C 文件二进制读写

C++/C 文件二进制读写

原创 2014年08月20日 18:44:46

C++方式 

  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <time.h>  
  5. #include<sys/time.h>  
  6. #include<unistd.h>  
  7. #include <iostream>  
  8. #include <fstream>  
  9. #include <string.h>  
  10. #include<vector>  
  11. #include <string.h>  
  12. #include <sys/types.h>  
  13. #include <dirent.h>  
  14. #include <sys/stat.h>  
  15. using namespace std;  
  16.   
  17. typedef unsigned long long int uint64;  
  18. typedef unsigned int uint32;  
  19. int fileread(char* location);  
  20.   
  21.   
  22. int main() {  
  23.     cout << ”!!!Hello World!!!” << endl; // prints !!!Hello World!!!  
  24.     char* path =  “/home/lhb/share/data/1/10600000.bin”;  
  25.     fileread(path);  
  26.     return 0;  
  27. }  
  28.   
  29. int fileread( char* location)  
  30. {  
  31.     //读文件  
  32.     ifstream fin(location, ios::binary);  
  33.     if(!fin.is_open())  
  34.     {  
  35.         printf(”Fail to open the file %s”,location);  
  36.         return -1;  
  37.     }  
  38.   
  39.     uint64 p = 0;  
  40.     float a = 0;  
  41.     fin.read((char*)&a, sizeof(uint32));  
  42.     float version = (float) (a);  
  43.     cout<<”该程序版本为 ”<<a<<endl;  
  44.     fin.read((char*)&p, sizeof(uint32));  
  45.     int type = (int) (p);  
  46.     cout<<”该程序类型为 ”<<p<<endl;  
  47.     fin.read((char*)&p, sizeof(uint32));  
  48.     int dim = (int) (p);//每个数据长度  
  49.     cout<<”每个数据占”<<dec<<p<<“个bit”<<endl;  
  50.     fin.read((char*)&p, sizeof(uint32));  
  51.     int feature_num = (int) (p);//数据个数  
  52.     cout<<” This file has ”<<dec<<p<<“ data.”<<endl;  
  53.   
  54. //  
  55. //   for( int i = data_counter ; i < (data_counter+feature_num) ; i++)  
  56. //   {  
  57. //          fin.read((char *)&szBuf[i][0],4 );  
  58. //          for(int j = 1; j < 5; j ++)  
  59. //          {  
  60. //              fin.read((char *)&szBuf[i][j],8);  
  61. //          }  
  62. //  
  63. //    }  
  64. //  data_counter+= feature_num;  
  65.     fin.close();  
  66.     return feature_num;  
  67. }  
#include <iostream>




#include <stdio.h> #include <stdlib.h> #include <time.h> #include<sys/time.h> #include<unistd.h> #include <iostream> #include <fstream> #include <string.h> #include<vector> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> using namespace std; typedef unsigned long long int uint64; typedef unsigned int uint32; int fileread(char* location); int main() { cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! char* path = "/home/lhb/share/data/1/10600000.bin"; fileread(path); return 0; } int fileread( char* location) { //读文件 ifstream fin(location, ios::binary); if(!fin.is_open()) { printf("Fail to open the file %s",location); return -1; } uint64 p = 0; float a = 0; fin.read((char*)&a, sizeof(uint32)); float version = (float) (a); cout<<"该程序版本为 "<<a<<endl; fin.read((char*)&p, sizeof(uint32)); int type = (int) (p); cout<<"该程序类型为 "<<p<<endl; fin.read((char*)&p, sizeof(uint32)); int dim = (int) (p);//每个数据长度 cout<<"每个数据占"<<dec<<p<<"个bit"<<endl; fin.read((char*)&p, sizeof(uint32)); int feature_num = (int) (p);//数据个数 cout<<" This file has "<<dec<<p<<" data."<<endl; // // for( int i = data_counter ; i < (data_counter+feature_num) ; i++) // { // fin.read((char *)&szBuf[i][0],4 ); // for(int j = 1; j < 5; j ++) // { // fin.read((char *)&szBuf[i][j],8); // } // // } // data_counter+= feature_num; fin.close(); return feature_num; }

*: fin.read的第一个参数必须为char*,所以将变量取&后强制转换后传入。后面循环实现读取数据块。

缺点:只有一个参数控制读入长度,当读取的数据类型和存放的数据类型不一致时只能分多次实现。

http://www.360doc.com/content/10/1207/12/963301_75777844.shtml

http://blog.csdn.net/kingstar158/article/details/6859379



C语言方式

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4. #include<sys/time.h>  
  5. #include<unistd.h>  
  6. #include <iostream>  
  7. #include <fstream>  
  8. #include <string.h>  
  9. #include<vector>  
  10. #include <string.h>  
  11. #include <sys/types.h>  
  12. #include <dirent.h>  
  13. #include <sys/stat.h>  
  14. typedef unsigned int uint32;  
  15. typedef unsigned long long int uint64;  
  16.   
  17. using namespace std;  
  18.   
  19.   
  20. int main()  
  21. {  
  22. FILE * stream;  
  23. stream = fopen(”/home/lhb/share/data/1/200000.bin”,“rb+”);  
  24. if (stream)  
  25. {  
  26.     cout<<”seccess”<<endl;  
  27. }  
  28.   
  29.     uint64 p=0;  
  30.     float a=0;  
  31.     fread(&a,sizeof(float),1,stream);  
  32.     cout<<a<<endl;  
  33.     fread(&p,sizeof(uint32),1,stream);  
  34.     cout<<p<<endl;  
  35.     fread(&p,sizeof(uint32),1,stream);  
  36.     cout<<p<<endl;  
  37.     fread(&p,sizeof(uint32),1,stream);  
  38.     cout<<p<<endl;  
  39.   
  40.   
  41. fclose(stream);  
  42.   
  43. }  
#include <stdio.h>




#include <stdlib.h> #include <time.h> #include<sys/time.h> #include<unistd.h> #include <iostream> #include <fstream> #include <string.h> #include<vector> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> typedef unsigned int uint32; typedef unsigned long long int uint64; using namespace std; int main() { FILE * stream; stream = fopen("/home/lhb/share/data/1/200000.bin","rb+"); if (stream) { cout<<"seccess"<<endl; } uint64 p=0; float a=0; fread(&a,sizeof(float),1,stream); cout<<a<<endl; fread(&p,sizeof(uint32),1,stream); cout<<p<<endl; fread(&p,sizeof(uint32),1,stream); cout<<p<<endl; fread(&p,sizeof(uint32),1,stream); cout<<p<<endl; fclose(stream); } *fread()第一个参数为void*,也许要&转换

优点:后面有两个参数控制读取长度,第二参数为每次读取的字节数,第三参数为读取的次数。

http://blog.csdn.net/AresGod/article/details/1852321

版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 本文已收录于以下专栏:

相关文章推荐

C/C++二进制读写png文件

为了弄OpenGl的纹理代码,发现书上没有图片像素的获取,然后就想写个来获取png的,结果花了一天的时间没弄清楚为什么出现数据个别正确其他的却是205,突然想起来以前弄软工的时候虽然那个网站只完成了登…

python/c/c++ 二进制文件读写

1.python 二进制文件的读写只讨论二进制文件的读写。 以二进制的形式打开文件with open(filename , ‘wb’) as fd : #do with fd f…
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup=window.slotbydup || []).push({ id: '4765209', container: s, size: '808,120', display: 'inlay-fix' }); })();

[Matlab+C/C++] 读写二进制文件

在处理某些数据的时候,可能涉及到文件的读写,如果用MATLAB存储为mat文件,那么其它程序读取这种数据就变得困难了。如果将数据存为文本文件,文件的解析过程就会变得比较长。幸运的是MATLAB可以读写…

c/c++ 二进制文件读写

1、windows api(MFC)
#include //#include #include //头文件也可以使用#include,而不用#include 但代码中的CString都要换成c…

C/C++文件的操作–二进制文件读写

int a=1; float b= 1.2; FILE *pOutFile = fopen( “你的文件名 “, “w+b ” ); BYTE* temp;…

c++二进制文件读写

C/C++读写注册表中二进制数据【代码示例】

使用Windows API 函数中的RegOpenKeyEx()函数和RegSetEx()函数来实现对注册表某项下的所有子项进行枚举。 1、RegOpenKeyEx 函数: 原形:LO…
  • lhyhr
  • lhyhr
  • 2014-11-17 17:28
  • 1267

C++读写二进制文件

摘要: 使用C++操作文件,是研发过程中比较频繁的,因此进行必要的总结和封装还是十分有用的。今天在网上找到一篇,遂进行了部分的试验,以记之,备后用。   本文读写文件均使用文件流进行操作,主要使用…

C++读写二进制文件

C++读写二进制文件,二进制文件的复制

C++读写二进制文件

C++提供了以下类执行从文件输出字符或者输入字符到文件里: ofstream:写文件的Stream 类 ifstream:读取文件的Stream 类: fstream:读/写文件的Stream 类。

C++读写二进制文件

摘要:使用C++读写二进制文件,在开发中操作的比较频繁,今天有幸找到一篇文章,遂进行了一些试验,并进行了部分的总结。使用C++操作文件,是研发过程中比较频繁的,因此进行必要的总结和封装还是十分有用的。…

c++读写二进制文件

c++读写二进制文件,在windows与linux下可能会有不同的效果。本人写的一个小例子在linux下写入二进制数据正常,而在windows下面写入数据的过程中,竟然自动添加了0x25等等一些无意义…

C++ Mat图像到二进制文件的读写

本文主要看到下面链接的文章, 在作者的基础上做了一些修改,然后整理成两个函数。 感谢原文作者 http://blog.csdn.net/yhl_leo/article/details/5078279…

C++二进制文件读写

今天在做项目时联想到了这两个问题,所以实际编程测试了一下,有一些新的收获:    我一直以为自己很熟悉如何使用C/C++中的二进制文件,可今天测试的时候突然发现程序生成的二进制文件和文本文件一样。比…

c文件操作-二进制文件读写

上次已经提到过了文件基本分为二进制文件和文本文件,文本文件是人可以直接读的懂的以文字的方式表达出来的文件,二二进制文件则需要机器以特定的方式或者软件来打开,比如音频视频文件都是二进制的。 今天我们通…

《C++语言基础》程序阅读——二进制文件及文件的随机读写

返回:贺老师课程教学链接1、阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。查看其内容,并理解文件…

《C++语言基础》程序阅读——二进制文件及文件的随机读写(4)

4、阅读并运行下面的示例,体会二进制文件和字符串流操作的一般方法。  例16
#include #include #include using namespace std; struct stude…

15周《C++语言基础》程序阅读——二进制文件及文件的随机读写(2)(3)

2、查看下面程序的输出,解释为什么会有这样的输出。
#include #include using namespace std; int main( ) { unsigned char …

【C/C++学院】0826-文件重定向/键盘输入流/屏幕输出流/字符串输入输出/文件读写简单操作/字符文件读写二进制与文本差别/get与getline挖掘数据/二进制与文本差别/随机位置/多线程初级

文件重定向 [java] view plaincopy
#include   using namespace std;  …

JAVA读写二进制文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值