存文件太大--换至纯C读写文件方法

存储的文件大小是原来保存txt的两倍多,错误!

int _tmain(int argc, _TCHAR* argv[])
{
	static float ATAN2_TABLE[512][512] = { { 0 } };

	
	std::ofstream file_atan("Table.bin", std::ios::binary);

	// Fill the atan2 table
#pragma omp critical
	if (ATAN2_TABLE[0][0] == 0) {
		for (int dy = -255; dy <= 255; ++dy) {
			for (int dx = -255; dx <= 255; ++dx) {
				// Angle in the range [-pi, pi]
				double angle = atan2(static_cast<double>(dy), static_cast<double>(dx));

				// Convert it to the range [9.0, 27.0]
				angle = angle * (9.0 / M_PI) + 18.0;

				// Convert it to the range [0, 18)
				if (angle >= 18.0)
					angle -= 18.0;

				ATAN2_TABLE[dy + 255][dx + 255] = max(angle, 0.0);
				printf("%f\t", ATAN2_TABLE[dy + 255][dx + 255]);

				char s[20] = {0};
				const char tmp[1] = {','};
				
				sprintf_s(s, "%f", ATAN2_TABLE[dy + 255][dx + 255]);

				/*file_atan.write(s, sizeof(char)*(sizeof(s)));
				file_atan.write(tmp, sizeof(char)*(sizeof(tmp)));*/
	
			}
		}
	}

	file_atan.close();

	return 0;
}

以下完整代码可缩小文件大小到一倍多,不错呀!

int _tmain(int argc, _TCHAR* argv[])
{
	static float ATAN2_TABLE[512][512] = { { 0 } };

		FILE *fp;
		fp = fopen("table2.bin", "wb");
		
		//std::ofstream file_atan("Table.bin", std::ios::binary);
	
		// Fill the atan2 table
	#pragma omp critical
		if (ATAN2_TABLE[0][0] == 0) {
			for (int dy = -255; dy <= 255; ++dy) {
				for (int dx = -255; dx <= 255; ++dx) {
					// Angle in the range [-pi, pi]
					double angle = atan2(static_cast<double>(dy), static_cast<double>(dx));
	
					// Convert it to the range [9.0, 27.0]
					angle = angle * (9.0 / M_PI) + 18.0;
	
					// Convert it to the range [0, 18)
					if (angle >= 18.0)
						angle -= 18.0;
	
					ATAN2_TABLE[dy + 255][dx + 255] = max(angle, 0.0);
					printf("%f\t", ATAN2_TABLE[dy + 255][dx + 255]);
					float tt[] = { ATAN2_TABLE[dy + 255][dx + 255] };
	
					fwrite(tt, 1, sizeof(tt), fp);
	
				}
			}
		}
	
		fclose(fp); // 关闭文件


	FILE *fp0;
	fp0 = fopen("table2.bin", "rb"); // 重新打开文件读操作
	//fstream fs;
	//fs.open("tmp.txt", ios::out);

	static float ATAN22_TABLE[512][512] = { { 0 } };

	for (int dy = -255; dy <= 255; ++dy) {
		for (int dx = -255; dx <= 255; ++dx) {
			float tt[1] = {0};
			fread(tt, 1, sizeof(tt), fp0); // 从文件中读数据
			printf("%f\t", tt[0]);

			ATAN22_TABLE[dy][dx] = tt[0];

			//验证代码
			//char s[20] = { 0 };
			//const char tmp[1] = { ',' };
			//sprintf_s(s, "%f", tt[0]);
			//fs.write(s, 9);
			//fs.write(",", 1);
		}
	}
	
	fclose(fp0); // 关闭文件
	//fs.close();

	return 0;
}

参考链接:http://zhidao.baidu.com/link?url=FbLvzyC2fRq2l_SXDxoUtqpEKQHF4Aw-QFldOtWVQChbAXpaVgssDboge9McBwMohROXoYKffebQaHBvDSsKCK

参考链接:http://c.biancheng.net/cpp/html/257.html

参考链接:http://www.cnblogs.com/emanlee/p/4418163.html


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值