Reputation: 3697
I need to read a file with mixed binary and ASCII data using C++ and pure std(no Boost). This data usually contains a mix of nested binary and ASCII headers, followed by binary data and comma separated data. The data is not well aligned, and the binary headers define the number of bytes of the ASCII data, which could be variable from header to header. A section of the data would look like this:
[binary header][binary data]The,ASCII,data,[binary header][binary data]is,not,continuous,...
I found good tutorials using read for binary data and getline for ASCII data, and I know that I could read byte by byte using chars, but I was wondering if any of you had to deal with this situation before, and had any useful suggestions on the best way to approach this problem.
Upvotes: 3
Views: 5057
Reputation: 153909
The only real problem is knowing where you change between the formats in the file. You'll have to open the file in binary mode (but that's usually the case for portable files anyway). Beyond that, you have to recognize when to switch between the binary format (which you'll have to implement, because there isn't one in the standard) and the text format.
Upvotes: 1
Reputation: 355
If you want to parse a file, you need to know beforehand how it is constructed.
While one file can be a table with the columns separated by commas and rows separated by new lines, another file might be an XML or fully binary with specific field definitions.
If you have a mixture of binary and ASCII data, you must read it all as a binary file.
ASCII characters are no more than binary numbers that represent readable characters.
So you need to know in advance how the file is built and what is the meaning of each byte in the file.
One common way to read such a file is creating a structure with the fields in the file and just reading the file into it.
For example:
#include <stdio.h>
#include <stdlib.h>
typedef struct mystruct_st
{
int var1;
char var2;
char text[10];
}mystruct_t;
mystruct_t mystruct;
int main () {
FILE * pFile;
pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
fread (&mystruct,sizeof(mystruct_t),1,pFile);
fclose (pFile);
return 0;
}
Upvotes: 3