存储的文件大小是原来保存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