博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[C++] c++中二进制文件的创建与使用
阅读量:5257 次
发布时间:2019-06-14

本文共 5120 字,大约阅读时间需要 17 分钟。

二进制文件和文本文件是不同的文件类型,因此在创建等方式也是不一样的

使用文件方式见下表:

"r"(只读)        为输入打开一个文本文件    

"w"(只写)      为输出打开一个文本文件    

"a"(追加)       为追加打开一个文本文件    

"rb"(只读)      为输入打开一个二进制文件    

"wb"(只写)     为输出打开一个二进制文件    

"ab"(追加)      为追加打开一个二进制文件    

"r+"(读写)      为读/写打开一个文本文件    

"w+"(读写)     为读/写创建一个文本文件    

"a+"(读写)      为读/写打开一个文本文件    

"rb+"(读写)     为读/写打开一个二进制文件    

"wb+"(读写)   为读/写创建一个二进制文件    

"ab+"(读写)    为读/写打开一个二进制文件    

void generateBin(string filename, int npattern ,int ninput, int noutput){	FILE* file;	file = fopen(filename.c_str(), "w");	if (file == NULL)	{		throw runtime_error("network: could not open " + string(filename) + " for writing");	}	fwrite(&npattern, sizeof(int), 1, file);	fwrite(&ninput, sizeof(int), 1, file);	fwrite(&noutput, sizeof(int), 1, file);	float * input = new float[ninput];	float * output = new float[noutput];	float range = 1;	for (int n = 0; n < npattern;n++){		for (int i = 0; i < ninput; i++){			input[i] = range * ((float)rand() / RAND_MAX - 0.5);		}		fwrite(input,sizeof(float),ninput,file);		for (int o = 0; o < noutput; o++){			output[o] = range * ((float)rand() / RAND_MAX - 0.5);		}		fwrite(output, sizeof(float), noutput, file);	}	if (fclose(file) != 0) {		throw runtime_error("network: error on saving to " + string(filename));	}	delete input;	delete output;}void readdata(){	int ninput =0;	int noutput = 0;	int npatterns = 0;	size_t data_read = 0;	float* inputs = new float[ninput];	float* targets = new float[noutput];	string filename = "data.bin";	FILE *file = fopen(filename.c_str(), "r");	if (file == NULL)		throw runtime_error("Cannot open file " + filename + " for reading");	int ok =		fread(&npatterns, sizeof(int), 1, file) &&		fread(&ninput, sizeof(int), 1, file) &&		fread(&noutput, sizeof(int), 1, file);	cout << npatterns << " " << ninput << " " << noutput << endl;	cout << "ninput is:" << ninput << endl;	cout << "noutput is:" << noutput << endl;	cout << "npatterns is:" << npatterns << endl;	for (int i = 0; i < npatterns; i++) {		cout << "load_patterns i:" << i << endl;		data_read = fread(inputs, sizeof(float), ninput, file);		cout << "need: " << ninput << " get: " << data_read << endl;		if (data_read != ((size_t)ninput)) {			cout << "Wrong file format " << endl;			fclose(file);			throw runtime_error("Wrong file format");		}		data_read = fread(targets, sizeof(float), noutput, file);		if (data_read != ((size_t)noutput)) {			cout << "Wrong file format " << endl;			fclose(file);			throw runtime_error("Wrong file format");		}		cout << "load_patterns i:" << i << endl;	}	delete inputs;	delete targets;	fclose(file);}

  上面两段程序,试图创建一个二进制文件,并且向其中写入data,并且再在其中读出二进制数据。

但是运行的结果是错误的,

data_read 的数量小于ninput,原因是因为使用了文本文件的创建方式,但是确使用了二进制文件的操作。应该改为如下的代码
void generateBin(string filename, int npattern ,int ninput, int noutput){    FILE* file;    file = fopen(filename.c_str(), "wb");    if (file == NULL)    {        throw runtime_error("network: could not open " + string(filename) + " for writing");    }    fwrite(&npattern, sizeof(int), 1, file);    fwrite(&ninput, sizeof(int), 1, file);    fwrite(&noutput, sizeof(int), 1, file);    float * input = new float[ninput];    float * output = new float[noutput];    float range = 1;    for (int n = 0; n < npattern;n++){        for (int i = 0; i < ninput; i++){            input[i] = range * ((float)rand() / RAND_MAX - 0.5);        }        fwrite(input,sizeof(float),ninput,file);        for (int o = 0; o < noutput; o++){            output[o] = range * ((float)rand() / RAND_MAX - 0.5);        }        fwrite(output, sizeof(float), noutput, file);    }    if (fclose(file) != 0) {        throw runtime_error("network: error on saving to " + string(filename));    }    delete input;    delete output;}void readdata(){    int ninput =0;    int noutput = 0;    int npatterns = 0;    size_t data_read = 0;    float* inputs = new float[ninput];    float* targets = new float[noutput];    string filename = "data.bin";    FILE *file = fopen(filename.c_str(), "rb");    if (file == NULL)        throw runtime_error("Cannot open file " + filename + " for reading");    int ok =        fread(&npatterns, sizeof(int), 1, file) &&        fread(&ninput, sizeof(int), 1, file) &&        fread(&noutput, sizeof(int), 1, file);    cout << npatterns << " " << ninput << " " << noutput << endl;    cout << "ninput is:" << ninput << endl;    cout << "noutput is:" << noutput << endl;    cout << "npatterns is:" << npatterns << endl;    for (int i = 0; i < npatterns; i++) {        cout << "load_patterns i:" << i << endl;        data_read = fread(inputs, sizeof(float), ninput, file);        cout << "need: " << ninput << " get: " << data_read << endl;        if (data_read != ((size_t)ninput)) {            cout << "Wrong file format " << endl;            fclose(file);            throw runtime_error("Wrong file format");        }        data_read = fread(targets, sizeof(float), noutput, file);        if (data_read != ((size_t)noutput)) {            cout << "Wrong file format " << endl;            fclose(file);            throw runtime_error("Wrong file format");        }        cout << "load_patterns i:" << i << endl;    }    delete inputs;    delete targets;    fclose(file);}

 

转载于:https://www.cnblogs.com/deepblueme/p/5096366.html

你可能感兴趣的文章
【BZOJ】4565: [Haoi2016]字符合并
查看>>
1006 换个格式输出整数
查看>>
[翻译] Foo over UDP
查看>>
spring boot 入门 使用spring.profiles.active来分区配置
查看>>
【转载】构建高并发高可用的电商平台架构实践
查看>>
使用mysql函数 group_concat 一点需要注意的
查看>>
差分进化算法 DE-Differential Evolution
查看>>
JAVA I/O操作那些事之新I/O(nio)
查看>>
DS博客作业08--课程总结
查看>>
opencv读取并播放视频代码
查看>>
AIX 常用命令汇总
查看>>
Eclipse创建一个普通maven项目详细步骤
查看>>
HDU-3046 Pleasant sheep and big big wolf
查看>>
Jquery拖拽,拖动排序插件
查看>>
前端现状与趋势
查看>>
PS图层混合算法之一(不透明度,正片叠底,颜色加深,颜色减淡)
查看>>
字符串截取
查看>>
博客园win8客户端开发记录6 - 成功发布到微软应用商店
查看>>
【android】android对位图文件的支持
查看>>
mysql慢查询日志分析工具 mysqlsla(转)
查看>>