Now that I am quite familiar with Python, decided to learn C++, so I am very n00b but sure willing to learn. I had made a script to read from a very tightly-specified file format (.EDF, for medical signals), with an ascii header defined by field sizes in bytes. So, I read 8 bytes for the first field, 80 bytes for the second field, and so on.
My working python script is as follows:
## HEADER FIELD NAMES AND SIZES FROM EDF SPEC:
header_fields = (
('version', 8), ('patinfo', 80), ('recinfo', 80),
('start date', 8), ('start time', 8), ('header bytes', 8),
('reserved', 44), ('nrecs', 8), ('recduration', 8),
('nchannels', 4))
## TELL WHICH FILE TO OPEN
folder = os.path.expanduser('~/Dropbox/01MIOTEC/06APNÉIA/Samples')
f = open(folder + '/Osas2002plusQRS.rec', 'rb')
# READ FILE CONTENT TO DICTIONARY OF LABELLED FIELD CONTENTS,
# ALREADY STRIPPED FROM BLANK SPACES
header = {}
for key, value in header_fields:
header[key] = f.read(value).strip()
The end result is 'header', a dictionary where each pair is a "labeled" string.
My current awkward c++ code, which almost work printing to screen the unstripped strings, is this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
static int header_bytes[] = {8,80,80,80,80,8,8,8,44,8,8,4};
static int header_bytes_len = sizeof(header_bytes)/sizeof(int);
static string header_fields[] =
{
"version",
"patinfo",
"recinfo",
"patinfo",
"recifo",
"start date",
"start time",
"header bytes",
"reserved",
"nrecs",
"rec duration",
"nchannels"
};
int main()
{
ifstream edfreader;
edfreader.open("/home/helton/Dropbox/01MIOTEC/06APNÉIA/Samples/Osas2002plusQRS.rec", ios::binary);
char * buffer = new char [80];
for (int n = 0; n<header_bytes_len; n++)
{
edfreader.read(buffer, header_bytes[n]);
buffer[header_bytes[n]] = '\0';
cout<<"'"<<buffer<<"'"<<endl;
}
return 0;
}
Actually, I copy-pasted the last part of main() from a cplusplus.com forum entry, just to get some kind of output, but actually what I wanted was to save the fields as an array of string objects, or better yet an array of pointers to string objects. I am reading "C++ Primer", but still in 200+ pages, but I want badly to fiddle with some c++ code fiddling, so if anyone could point me to some methods or concepts or readings, I would be very happy.
Thanks for reading