PDA

View Full Version : Read binary from file



weldpua2008
1st April 2009, 14:38
Hello All.
I have source of already wrote program on Borland C++, but I can't convert it to QT4.
I need read data from file. I include full overview about file format into zip file and less below.
I use mdi sample and rewrite function openFile(), and it's work:

void MdiChild::openFile(QString curFile){
isUntitled = true;
setWindowTitle(curFile + " ");
QFile file(curFile);
QApplication::setOverrideCursor(Qt::WaitCursor); //курсор в режим ожидания
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, tr("Spreadsheet"),
tr("Cannot open file %1:\n%2.")
.arg(curFile)
.arg(file.errorString()));
}
else
{
QDataStream in(&file);
}
QApplication::restoreOverrideCursor(); //курсор в нормальный режим
file.close();
}

But I don't know how write code witch I have already in Borland. I don't understand how read binary file. Maybe I don't need source on Borland, and I can write other program:

void TMainForm::vOpenDSPZdata(){
Chart->Title->Text->Clear();
Chart->Title->Text->Add("Converting to DSP data format");
int NF = OpenMultiDialog->Files->Count;

for (int nf=0;nf<NF;nf++){

FLName=OpenMultiDialog->Files->Strings[nf];
FH=FileOpen(FLName,fmOpenRead);
if(FH==-1){ShowMessage("Error with FileOpen");return;}

FSName = ExtractFileName(OpenMultiDialog->Files->Strings[nf]);
FSName = FSName.SubString(0,FSName.Length()-4)+".dsp";
FSName = ExtractFilePath(FLName) + FSName;
filec = 0;
file_razmer = FileSeek(FH,filec,2);
FileSeek(FH,0,0);
FileRead(FH,&Q.fh, sizeof Q.fh);
Q.fh2dspp(66000,0,0);
Q.allocate_spectr();
n_vib = (file_razmer - (sizeof Q.fh))/(sizeof 4*2*Q.nDSPZ);
Memo->Lines->Add(">Open file name : " + FLName);
Memo->Lines->Add(">Saving file name : " + FSName);
Memo->Lines->Add(">File size : " + IntToStr(file_razmer) + " Byte");
Memo->Lines->Add(">sizeof header : " + IntToStr(sizeof Q.fh) + " Byte");
Memo->Lines->Add(">sizeof spectr : " + IntToStr(sizeof 4*2*Q.nDSPZ) + " Byte");
Memo->Lines->Add(">Count of spectra : " + IntToStr(n_vib));
Memo->Lines->Add(">Count of chanel DSPZ: " + IntToStr(Q.nDSPZ));
Memo->Lines->Add(">Resolution DSPZ: " + FloatToStrF(Q.dspp.NAvr * Q.NDSPZ * 1./ Q.dspp.frcl,ffFixed,8,2) + " ms");
Memo->Lines->Add(">Averaging factor DSPZ: " + IntToStr(Q.dspp.NAvr));
Memo->Lines->Add(">Resolution DSP: " + FloatToStrF(Q.dspp.NAvr * Q.NDSPZ * 1./ Q.dspp.frcl , ffFixed,8,2) + " ms");
Memo->Lines->Add(Q.fh.time);
astr="";
for (i=0;i<16;i++){
astr+= IntToStr(i) + ": " + Q.fh.syst[i] + " ";
}
Memo->Lines->Add("> "+ astr);
Memo->Lines->Add(Q.fh.desc);
int Naver;
if (OF->nAverCB->Checked && n_vib>Naver)
Naver = StrToInt(OF->nAverEdit->Text);
else Naver = n_vib;
Memo->Lines->Add(">Number of spectr for DSP file: " + IntToStr(Naver));
Naver=10;
for(i=0;i<Naver;i++){
FileRead(FH,Q.spectr, sizeof 4*2*Q.nDSPZ);
Q.get_spectrAB_prop();
SeriesShow();
if (!((i+1)%100)){
Caption = CaptInfo + " file# " + IntToStr(nf+1) + "/" + IntToStr(NF) +
" - " + IntToStr(i*100/Naver) + "%";
SeriesShow();
Sleep(20);
Application->ProcessMessages();
}
}

Application->ProcessMessages();

FileClose(FH);
}//nf
}
Where DSPZ Q and DSP W

class DSP{
public:
DBUFFH fh;
DBUFSP sp;
int nDSP;
DSP::DSP();
DSP::~DSP();
int intLEtoBE(int le);
int intfLEtoBE(float fl);
float floatLEtoBE(int le);
float getchA(int k);
float getchB(int k);
private:
int i,j;
float tmp;

};

class DSPZ{
public:
int nDSPZ;
int NDSPZ;
DSP_FH fh;
int *spectr;
float *chA,*chB;
SPECTR_PROP spp;
DSPP_PROP dspp;
DSPZ::DSPZ();
DSPZ::~DSPZ();
void get_spectrAB_prop();
void allocate_spectr();
void fh2dspp(float freqclock, int winchA, int winchB);
private:
int i,j;
float tmp;
};

The file header is placed at begin of the file (0x0h offset). File header has size 1024 bytes and contains following fields:
In “C”-language notation header can by considered as structure:

typedef struct FHEADER_Tag
{
char name[32];
char time[32];
char gmtt[32];
char sysn[32];
char syst[32];
char place[96];
char desc[256];
DWORD PP[128];
DWORD DSPP[384];
} FHEADER;
In the “C”-language notation processing parameters structure PP can by considered as following structure:


typedef struct UP_EXCH_Tag //DSP parameters
{
DWORD mode; //mode of operation
DWORD size; //size of DMA data block
DWORD prc_mode; //processing parameters
DWORD tst_gen; //not used
DWORD clk; //not used
DWORD fft_size; //used for data trasferering
}UP_EXCH;
In the “C”-language notation processing parameters structure PP can by considered as following structure:

typedef struct DSPP_tag //DSP processing parameetrs
{
int FFT_Size; //for using in the DSP
int MinDSPSize; // for using in the DSP
int MinDMASize; // for using in the DSP
int DMASizeCnt; // for using in the DSP
int DMASize; // for using in the DSP
float CLCfrq;
int Synch;
int SSht;
int Mode;
int Wch;
int Smd;
int Offt,
int Lb;
int Hb;
int Wb;
int NAvr;
BOOL CAvr;
BOOL Weight;
BOOL DCRem;
BOOL ExtSyn;
BOOL Ch1;
BOOL Ch2;
BOOL ExtWin;
int Clip;
//RF parts
int HPF0;
int HPF1;
int LPF0;
int LPF1;
int ATT0;
int ATT1;
} DSPP;

Just after file header, the data follow. So, the data offset in the file is 0x400h. In the Spectrum Mode (Mode=1) the Wb pair of samples (spectrum lines) are stored. First sample is from A channel, and second sample is from B channel (see (2) and (4) on Fig.I.1). Just after 2*Wb samples of the spectrum the next 2*Wb spectrum samples are stored… and so on.

Each FFT sample (SSAMPLE) has 4 byte size SSAMPLE is in the DSP floating point format. To get the sample presented in conventional floating point format (FLOAT_NUM) one should apply the following transformation (in “C” notation):
Fig.1 Data sequence in the “Spectrum” mode

weldpua2008
1st April 2009, 15:53
I need read file which write by spatial hardware..

I read again http://doc.trolltech.com/4.5/qdatastream.html#details
I don't understand: can I use it to read this binary format?

Can I use like http://www.qtcentre.org/forum/p-how-to-write-a-qlist-into-a-binary-file-post97679/postcount9.html some structures and read binary file:


QDataStream in(&file);
in >> header >> channelA >>channalB>> channelA >>channalB>> channelA >>channalB;

weldpua2008
3rd April 2009, 23:50
I can read and I use this code:

void MdiChild::openFile(QString curFile){
isUntitled = true;
setWindowTitle(curFile + " ");
QFile file(curFile);
QApplication::setOverrideCursor(Qt::WaitCursor); //курсор в режим ожидания
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, tr("Spreadsheet"),
tr("Cannot open file %1:\n%2.")
.arg(curFile)
.arg(file.errorString()));
}
else
{
// QDataStream in(&file);
// file_razmer = FileSeek(FH,filec,2);
if (file.read(reinterpret_cast<char*>(&Q.fh), sizeof(Q.fh)) != sizeof(Q.fh))
{
QMessageBox::warning(this, tr("Spreadsheet"),
tr("Cannot read header of file %1:\n%2.")
.arg(curFile)
.arg(file.errorString()));
}
Q.fh2dspp(66000,0,0);
n_vib = (file.size() - (sizeof Q.fh))/(sizeof 4*2*Q.nDSPZ);
QString astr="";
for (i=0;i<16;i++){
astr+= QString::number(i) + ": " + QString::number(Q.fh.syst[i]) + " ";
}
int Naver;
/*if (OF->nAverCB->Checked && n_vib>Naver)
Naver = StrToInt(OF->nAverEdit->Text);
else Naver = n_vib;
*/
Naver = n_vib;
QMessageBox::warning(this, tr("Propertis"),
tr("File size %1\n %2:\n sizeof header %3\n sizeof spectr: %4 \n Count of spectra %5 \n Count of chanel DSPZ %6 \n Averaging factor DSPZ: %7 \n %8 \n %9 \n %10 \n Number of spectr for DSP file: %11\n ")
.arg(file.size())
.arg(curFile)
.arg(QString::number(sizeof Q.fh) )
.arg(QString::number(sizeof 4*2*Q.nDSPZ) )
.arg(QString::number(sizeof n_vib) )
.arg(QString::number(sizeof Q.nDSPZ) )
.arg(QString::number(Q.dspp.NAvr) )
.arg(Q.fh.time)
.arg(astr)
.arg(Q.fh.desc)
.arg(QString::number(Naver) )
);
Naver=10;
for(i=0;i<Naver;i++){
file.read(reinterpret_cast<char*>(&Q.spectr), sizeof(4*2*Q.nDSPZ));
//Q.get_spectrAB_prop();
//SeriesShow();
}
}
QApplication::restoreOverrideCursor(); //курсор в нормальный режим
file.close();
}