C++ C++;双重保存/加载(到文件/从文件)
[为清晰起见,重新编写。] 我需要以一种总是具有相同字符数的格式在文件中写入和读取C++ C++;双重保存/加载(到文件/从文件),c++,double,mingw,standards,C++,Double,Mingw,Standards,[为清晰起见,重新编写。] 我需要以一种总是具有相同字符数的格式在文件中写入和读取doubles。格式不需要是人类可读的:它只需要快速加载(尽可能少的动态内存和转换内容,文件空间很重要,但并不重要) 是否有一种标准(或至少是安全可靠的)方法来获取double的组件,以便我可以将signiciand符号和尾数符号存储为'1'或'0',并将有效位和尾数分别存储为长度恒定的十六进制格式 从本质上讲,我如何从double中获取特定的位/数组件?甚至可以在单独的系统上执行此操作(假设相同的操作系统系列,如
double
s。格式不需要是人类可读的:它只需要快速加载(尽可能少的动态内存和转换内容,文件空间很重要,但并不重要)
是否有一种标准(或至少是安全可靠的)方法来获取double
的组件,以便我可以将signiciand符号和尾数符号存储为'1'
或'0'
,并将有效位和尾数分别存储为长度恒定的十六进制格式
从本质上讲,我如何从double中获取特定的位/数组件?甚至可以在单独的系统上执行此操作(假设相同的操作系统系列,如Windows),还是每个操作系统都不强制执行double
s组件的标准
我正在使用MinGW,当然也在为Windows编译。我想在可能的地方使用C标准库,而不是C++标准库。此外,我希望避免使用其他库(如Boost),但如果有特定的Windows函数,则这些函数将非常有用。您可能需要这样的功能:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
template <typename T>
string convertToHex(const T &x)
{
char *xc = (char *)&x;
ostringstream s;
for (char *c = xc; c < xc + sizeof(x); ++c)
s << hex << setw(2) << setfill('0') << static_cast<int>(*c) << " ";
return s.str();
}
template <typename T>
void convertFromHex(string s, T &x)
{
char *xc = (char *)&x;
istringstream is(s);
for (char *c = xc; c < xc + sizeof(x); ++c)
{
int tmp;
is >> hex >> tmp;
*c = tmp;
}
}
int main()
{
double a = 10;
string as = convertToHex(a);
cout << "a: " << as << endl;
double b;
convertFromHex(as, b);
cout << "b: " << b << endl;
}
也许你想要这样的东西:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
template <typename T>
string convertToHex(const T &x)
{
char *xc = (char *)&x;
ostringstream s;
for (char *c = xc; c < xc + sizeof(x); ++c)
s << hex << setw(2) << setfill('0') << static_cast<int>(*c) << " ";
return s.str();
}
template <typename T>
void convertFromHex(string s, T &x)
{
char *xc = (char *)&x;
istringstream is(s);
for (char *c = xc; c < xc + sizeof(x); ++c)
{
int tmp;
is >> hex >> tmp;
*c = tmp;
}
}
int main()
{
double a = 10;
string as = convertToHex(a);
cout << "a: " << as << endl;
double b;
convertFromHex(as, b);
cout << "b: " << b << endl;
}
最直接的方法是以二进制模式打开fstream,然后使用fstream的write()和read()方法从流中读取您的双精度数据:
#include <fstream>
#include <iostream>
int main( int argc, char** argv ) {
std::fstream fp( "foo", std::fstream::in |
std::fstream::out |
std::fstream::trunc |
std::fstream::binary );
double d1, d2;
d1 = 3.14;
fp.write( (char*)&d1, sizeof( d1 ) );
fp.seekg( 0, std::fstream::beg );
fp.read( (char*)&d2, sizeof( d2 ) );
std::cout << "d1 = " << d1 << " d2 = " << d2 << std::endl;
}
#包括
#包括
int main(int argc,字符**argv){
std::fstream fp(“foo”,std::fstream::in|
std::fstream::out|
std::fstream::trunc|
std::fstream::binary);
双d1,d2;
d1=3.14;
fp.write((char*)&d1,sizeof(d1));
fp.seekg(0,std::fstream::beg);
fp.read((char*)&d2,sizeof(d2));
std::cout最直接的方法是以二进制模式打开fstream,然后使用fstream的write()和read()方法从流中读取双精度数据:
#include <fstream>
#include <iostream>
int main( int argc, char** argv ) {
std::fstream fp( "foo", std::fstream::in |
std::fstream::out |
std::fstream::trunc |
std::fstream::binary );
double d1, d2;
d1 = 3.14;
fp.write( (char*)&d1, sizeof( d1 ) );
fp.seekg( 0, std::fstream::beg );
fp.read( (char*)&d2, sizeof( d2 ) );
std::cout << "d1 = " << d1 << " d2 = " << d2 << std::endl;
}
#包括
#包括
int main(int argc,字符**argv){
std::fstream fp(“foo”,std::fstream::in|
std::fstream::out|
std::fstream::trunc|
std::fstream::binary);
双d1,d2;
d1=3.14;
fp.write((char*)&d1,sizeof(d1));
fp.seekg(0,std::fstream::beg);
fp.read((char*)&d2,sizeof(d2));
std::cout这里是一个非常简单的例子,使用了boost::serializer
()。我正在使用boost::archive::text\u-iarchive
和boost::archive::text\u-oarchive
,但是你可以将它切换到boost::archive::binary\u-iarchive
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;
int main()
{
double a = M_PI;
string text;
{
ostringstream textStream;
boost::archive::text_oarchive oa(textStream);
oa << a;
text = textStream.str();
}
cout << "a: " << text << endl;
double b;
{
istringstream textStream(text);
boost::archive::text_iarchive ia(textStream);
ia >> b;
}
cout << "b: " << b << endl;
}
下面是一个非常简单的例子,使用的是boost::serializer
()。我使用的是boost::archive::text\u-oarchive
和boost::archive::text\u-oarchive
,但是你可以将它切换到boost::archive::binary\u-oarchive
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;
int main()
{
double a = M_PI;
string text;
{
ostringstream textStream;
boost::archive::text_oarchive oa(textStream);
oa << a;
text = textStream.str();
}
cout << "a: " << text << endl;
double b;
{
istringstream textStream(text);
boost::archive::text_iarchive ia(textStream);
ia >> b;
}
cout << "b: " << b << endl;
}
为什么你不只是使用FStand LIB?你可以用FScript编写和读取双工,或者你希望它不是组件的整数?我宁愿使用C标准库而不是C++的一个库。“我宁愿使用C标准库而不使用C++标准库。"C++ C++是指C++的语言……但是如果标签是误导性的,我会改变为什么你不只是使用fStfLIB?你可以用FScript编写和读取双工,还是你想把它作为组件而不是整数?我宁愿使用C++标准库而不使用C++。是的,我宁愿拥有<代码>双<代码>的组成部分,而不是一个字中所写的数字。“我宁愿使用C标准库而不使用C++”。C++ C++是指C++的语言……但是我的意思是我使用C++语言…但是如果标签有误导性,我会很好地改变它,这将列出字节,但是我担心我会面临不同系统上的排序问题。@ RSETC你的要求并不十分清楚,在这种情况下你可能需要。boost::serialization很好,这可以列出字节,但我担心在不同的系统上排序时可能会遇到问题。@rsethc您的要求不是很清楚。在这种情况下,您可能需要boost::serialization是否有类似的使用cstdio的方法?是否有类似的使用cstdio的方法?