Ich habe eine binäre Datei, die das folgende Format hat:Doppel Werte und int Werte von Binärdatei Lesen
# vtk DataFile Version 4.0
vtk output
BINARY
DATASET POLYDATA
POINTS 10000 double
?�T�����?����h�?�T�����?���� <-- 10000 double values (in binary format) follow separated by space and new line after every 9 values.
Ich mag diese Datei Byte für Byte lesen, so dass ich diese doppelten Werte in meinem Array speichern kann . Ich habe den folgenden Code, der diese Datei in ein char * -Puffer-Array lädt. Jetzt möchte ich wissen, wie man weiter vorgeht?
#include<iostream>
#include<fstream>
#include<sstream>
#include<stdlib.h>
#include<string>
using namespace std;
int main() {
ifstream is ("Data_binary.vtk", ifstream::binary);
if (is) {
// get length of file:
is.seekg (0, is.end);
unsigned long length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length+1];
buffer[length] = '\0';
cout << "Reading " << length << " characters... ";
// read data as a block:
is.seekg(0, is.beg);
is.read (buffer,length);
if (is)
cout << "all characters read successfully." << endl;
else
cout << "error: only " << is.gcount() << " could be read";
is.close();
}
return 0;
}
Im ASCII-Format wäre eine Beispieldatei wie folgt aussehen:
# vtk DataFile Version 4.0
vtk output
ASCII
DATASET POLYDATA
POINTS 18 double
.1 .2 .3 1.4 11.55 1 0 8e-03 5.6
1.02 2.2 3.3 .1 .5 0.001 4e-07 4.2 1.55
Für binäre Datei, die doppelten Werte in binärer vorhanden sind. Ich möchte doppelte Werte aus dem Binärformat erhalten.
Insbesondere [@ Sehes Antwort] (http://stackoverflow.com/a/7828841/1413395) sieht aus wie der Weg zu gehen. Es muss jedoch 'boost :: spirit' verwendet werden. –
@ πάντα ῥεῖ Ich habe Daten in Binärdatei. Ich möchte lernen, Binärdaten byteweise zu lesen, um Zeichen zu erhalten, oder Bytefolgen zu lesen, um doppelte/ganzzahlige Werte zu erhalten. – Jaipreet
Das ist keine Binärdatei. Was Sie beschreiben, ist genau das '.vdk' Dateiformat (siehe [hier] (http://dunne.uni-hd.de/VisuSimple/documents/vtkfileformat.html) bitte) –