मैं एक बाइनरी फ़ाइल में युगल के वेक्टर लिखने की कोशिश कर रहा हूं। ऐसा करने के बाद मैं इसे पढ़ना चाहता हूं। यह काम नहीं लग रहा है।लेखन वेक्टर <double> बाइनरी फ़ाइल में और इसे फिर से पढ़ने
ofstream bestand;
vector<double> v (32);
const char* pointer = reinterpret_cast<const char*>(&v[0]);
size_t bytes = v.size() * sizeof(v[0]);
bestand.open("test",ios::out | ios::binary);
for(int i = 0; i < 32; i++)
{
v[i] = i;
cout << i;
}
bestand.write(pointer, v.size());
bestand.close();
ifstream inlezen;
vector<double> v2 (32);
inlezen.open("test", ios::in | ios::binary);
char byte[8];
bytes = v2.size() * sizeof(v2[0]);
inlezen.read(reinterpret_cast<char*>(&v2[0]), bytes);
for(int i =0; i < 32; i++){
cout << endl << v2[i] << endl;
}
यह आउटपुट तो ऐसा लगता है कि यह सही ढंग पहले 4 नंबर लिखा है "0 1 2 3 0 0 0 ......": यहाँ कोड है।
'लिखना()' आकार तर्क गलत है। यह 'v.size() * आकार (डबल)' होना चाहिए। – hmjd
@ के-बॉलो 'skipws' unformatted इनपुट पर लागू नहीं होता है। –