C++/C 文件二进制读写
C++方式
- #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;
- }
#include <iostream>
*: fin.read的第一个参数必须为char*,所以将变量取&后强制转换后传入。后面循环实现读取数据块。
缺点:只有一个参数控制读入长度,当读取的数据类型和存放的数据类型不一致时只能分多次实现。
http://www.360doc.com/content/10/1207/12/963301_75777844.shtml
http://blog.csdn.net/kingstar158/article/details/6859379
- #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);
- }
#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
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 本文已收录于以下专栏:
![](http://avatar.csdn.net/9/F/6/1_woshixuexizhe10.jpg)
相关文章推荐
-
C/C++二进制读写png文件
为了弄OpenGl的纹理代码,发现书上没有图片像素的获取,然后就想写个来获取png的,结果花了一天的时间没弄清楚为什么出现数据个别正确其他的却是205,突然想起来以前弄软工的时候虽然那个网站只完成了登…- u014646950
- 2016-04-08 22:14
- 3544
-
python/c/c++ 二进制文件读写
1.python 二进制文件的读写只讨论二进制文件的读写。 以二进制的形式打开文件with open(filename , ‘wb’) as fd : #do with fd f…- liu2012huan
- 2016-11-14 17:21
- 1081
-
[Matlab+C/C++] 读写二进制文件
在处理某些数据的时候,可能涉及到文件的读写,如果用MATLAB存储为mat文件,那么其它程序读取这种数据就变得困难了。如果将数据存为文本文件,文件的解析过程就会变得比较长。幸运的是MATLAB可以读写…- bendanban
- 2016-03-08 01:08
- 2892
-
c/c++ 二进制文件读写
1、windows api(MFC)
- dslinmy
- 2014-08-07 10:47
- 615
-
C/C++文件的操作–二进制文件读写
int a=1; float b= 1.2; FILE *pOutFile = fopen( “你的文件名 “, “w+b ” ); BYTE* temp;…- bestdog
- 2011-08-04 18:17
- 8071
-
c++二进制文件读写
- 2012-12-18 08:48
- 89KB
- 下载
-
C/C++读写注册表中二进制数据【代码示例】
使用Windows API 函数中的RegOpenKeyEx()函数和RegSetEx()函数来实现对注册表某项下的所有子项进行枚举。 1、RegOpenKeyEx 函数: 原形:LO…- lhyhr
- 2014-11-17 17:28
- 1267
-
C++读写二进制文件
摘要: 使用C++操作文件,是研发过程中比较频繁的,因此进行必要的总结和封装还是十分有用的。今天在网上找到一篇,遂进行了部分的试验,以记之,备后用。 本文读写文件均使用文件流进行操作,主要使用…- lixiaoguang20
- 2016-12-23 14:55
- 88
-
C++读写二进制文件
C++读写二进制文件,二进制文件的复制- lightlater
- 2011-04-26 17:44
- 69855
-
C++读写二进制文件
C++提供了以下类执行从文件输出字符或者输入字符到文件里: ofstream:写文件的Stream 类 ifstream:读取文件的Stream 类: fstream:读/写文件的Stream 类。- laoxuan2011
- 2016-07-23 11:40
- 443
-
C++读写二进制文件
摘要:使用C++读写二进制文件,在开发中操作的比较频繁,今天有幸找到一篇文章,遂进行了一些试验,并进行了部分的总结。使用C++操作文件,是研发过程中比较频繁的,因此进行必要的总结和封装还是十分有用的。…- changbaolong
- 2011-07-22 23:22
- 6432
-
c++读写二进制文件
c++读写二进制文件,在windows与linux下可能会有不同的效果。本人写的一个小例子在linux下写入二进制数据正常,而在windows下面写入数据的过程中,竟然自动添加了0x25等等一些无意义…- qiaofangjie
- 2013-12-18 19:35
- 2152
-
C++ Mat图像到二进制文件的读写
本文主要看到下面链接的文章, 在作者的基础上做了一些修改,然后整理成两个函数。 感谢原文作者 http://blog.csdn.net/yhl_leo/article/details/5078279…- hust_bochu_xuchao
- 2016-11-24 14:57
- 1582
-
C++二进制文件读写
今天在做项目时联想到了这两个问题,所以实际编程测试了一下,有一些新的收获: 我一直以为自己很熟悉如何使用C/C++中的二进制文件,可今天测试的时候突然发现程序生成的二进制文件和文本文件一样。比…- generalhking
- 2012-09-25 12:09
- 2479
-
c文件操作-二进制文件读写
上次已经提到过了文件基本分为二进制文件和文本文件,文本文件是人可以直接读的懂的以文字的方式表达出来的文件,二二进制文件则需要机器以特定的方式或者软件来打开,比如音频视频文件都是二进制的。 今天我们通…- qq_20981209
- 2016-01-11 15:45
- 1369
-
《C++语言基础》程序阅读——二进制文件及文件的随机读写
返回:贺老师课程教学链接1、阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。查看其内容,并理解文件…- sxhelijian
- 2015-06-09 16:54
- 1572
-
《C++语言基础》程序阅读——二进制文件及文件的随机读写(4)
4、阅读并运行下面的示例,体会二进制文件和字符串流操作的一般方法。 例16
- zp___waj
- 2015-06-15 22:21
- 283
-
15周《C++语言基础》程序阅读——二进制文件及文件的随机读写(2)(3)
2、查看下面程序的输出,解释为什么会有这样的输出。
- zp___waj
- 2015-06-15 21:59
- 391
-
【C/C++学院】0826-文件重定向/键盘输入流/屏幕输出流/字符串输入输出/文件读写简单操作/字符文件读写二进制与文本差别/get与getline挖掘数据/二进制与文本差别/随机位置/多线程初级
文件重定向 [java] view plaincopy
- xiao_kou
- 2015-11-08 13:55
- 219
-
JAVA读写二进制文件
- 2016-01-10 19:07
- 1KB
- 下载