[C++] c++中二进制文件的创建与使用

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

使用文件方式见下表:

"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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值