+ Reply to Thread
Results 1 to 3 of 3

Thread: Read binary from file

  1. #1

    Unhappy Read binary from file

    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:
    Qt Code:
    1. void MdiChild::openFile(QString curFile){
    2. isUntitled = true;
    3. setWindowTitle(curFile + "[*]");
    4. QFile file(curFile);
    5. QApplication::setOverrideCursor(Qt::WaitCursor); //курсор в режим ожидания
    6. if(!file.open(QIODevice::ReadOnly))
    7. {
    8. QMessageBox::warning(this, tr("Spreadsheet"),
    9. tr("Cannot open file %1:\n%2.")
    10. .arg(curFile)
    11. .arg(file.errorString()));
    12. }
    13. else
    14. {
    15. QDataStream in(&file);
    16. }
    17. QApplication::restoreOverrideCursor(); //курсор в нормальный режим
    18. file.close();
    19. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. void TMainForm::vOpenDSPZdata(){
    2. Chart->Title->Text->Clear();
    3. Chart->Title->Text->Add("Converting to DSP data format");
    4. int NF = OpenMultiDialog->Files->Count;
    5.  
    6. for (int nf=0;nf<NF;nf++){
    7.  
    8. FLName=OpenMultiDialog->Files->Strings[nf];
    9. FH=FileOpen(FLName,fmOpenRead);
    10. if(FH==-1){ShowMessage("Error with FileOpen");return;}
    11.  
    12. FSName = ExtractFileName(OpenMultiDialog->Files->Strings[nf]);
    13. FSName = FSName.SubString(0,FSName.Length()-4)+".dsp";
    14. FSName = ExtractFilePath(FLName) + FSName;
    15. filec = 0;
    16. file_razmer = FileSeek(FH,filec,2);
    17. FileSeek(FH,0,0);
    18. FileRead(FH,&Q.fh, sizeof Q.fh);
    19. Q.fh2dspp(66000,0,0);
    20. Q.allocate_spectr();
    21. n_vib = (file_razmer - (sizeof Q.fh))/(sizeof 4*2*Q.nDSPZ);
    22. Memo->Lines->Add(">Open file name : " + FLName);
    23. Memo->Lines->Add(">Saving file name : " + FSName);
    24. Memo->Lines->Add(">File size : " + IntToStr(file_razmer) + " Byte");
    25. Memo->Lines->Add(">sizeof header : " + IntToStr(sizeof Q.fh) + " Byte");
    26. Memo->Lines->Add(">sizeof spectr : " + IntToStr(sizeof 4*2*Q.nDSPZ) + " Byte");
    27. Memo->Lines->Add(">Count of spectra : " + IntToStr(n_vib));
    28. Memo->Lines->Add(">Count of chanel DSPZ: " + IntToStr(Q.nDSPZ));
    29. Memo->Lines->Add(">Resolution DSPZ: " + FloatToStrF(Q.dspp.NAvr * Q.NDSPZ * 1./ Q.dspp.frcl,ffFixed,8,2) + " ms");
    30. Memo->Lines->Add(">Averaging factor DSPZ: " + IntToStr(Q.dspp.NAvr));
    31. Memo->Lines->Add(">Resolution DSP: " + FloatToStrF(Q.dspp.NAvr * Q.NDSPZ * 1./ Q.dspp.frcl , ffFixed,8,2) + " ms");
    32. Memo->Lines->Add(Q.fh.time);
    33. astr="";
    34. for (i=0;i<16;i++){
    35. astr+= IntToStr(i) + ": " + Q.fh.syst[i] + " ";
    36. }
    37. Memo->Lines->Add("> "+ astr);
    38. Memo->Lines->Add(Q.fh.desc);
    39. int Naver;
    40. if (OF->nAverCB->Checked && n_vib>Naver)
    41. Naver = StrToInt(OF->nAverEdit->Text);
    42. else Naver = n_vib;
    43. Memo->Lines->Add(">Number of spectr for DSP file: " + IntToStr(Naver));
    44. Naver=10;
    45. for(i=0;i<Naver;i++){
    46. FileRead(FH,Q.spectr, sizeof 4*2*Q.nDSPZ);
    47. Q.get_spectrAB_prop();
    48. SeriesShow();
    49. if (!((i+1)%100)){
    50. Caption = CaptInfo + " file# " + IntToStr(nf+1) + "/" + IntToStr(NF) +
    51. " - " + IntToStr(i*100/Naver) + "%";
    52. SeriesShow();
    53. Sleep(20);
    54. Application->ProcessMessages();
    55. }
    56. }
    57.  
    58. Application->ProcessMessages();
    59.  
    60. FileClose(FH);
    61. }//nf
    62. }
    To copy to clipboard, switch view to plain text mode 
    Where DSPZ Q and DSP W
    Qt Code:
    1. class DSP{
    2. public:
    3. DBUFFH fh;
    4. DBUFSP sp;
    5. int nDSP;
    6. DSP::DSP();
    7. DSP::~DSP();
    8. int intLEtoBE(int le);
    9. int intfLEtoBE(float fl);
    10. float floatLEtoBE(int le);
    11. float getchA(int k);
    12. float getchB(int k);
    13. private:
    14. int i,j;
    15. float tmp;
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class DSPZ{
    2. public:
    3. int nDSPZ;
    4. int NDSPZ;
    5. DSP_FH fh;
    6. int *spectr;
    7. float *chA,*chB;
    8. SPECTR_PROP spp;
    9. DSPP_PROP dspp;
    10. DSPZ::DSPZ();
    11. DSPZ::~DSPZ();
    12. void get_spectrAB_prop();
    13. void allocate_spectr();
    14. void fh2dspp(float freqclock, int winchA, int winchB);
    15. private:
    16. int i,j;
    17. float tmp;
    18. };
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. typedef struct FHEADER_Tag
    2. {
    3. char name[32];
    4. char time[32];
    5. char gmtt[32];
    6. char sysn[32];
    7. char syst[32];
    8. char place[96];
    9. char desc[256];
    10. DWORD PP[128];
    11. DWORD DSPP[384];
    12. } FHEADER;
    To copy to clipboard, switch view to plain text mode 
    In the “C”-language notation processing parameters structure PP can by considered as following structure:
    Qt Code:
    1. typedef struct UP_EXCH_Tag //DSP parameters
    2. {
    3. DWORD mode; //mode of operation
    4. DWORD size; //size of DMA data block
    5. DWORD prc_mode; //processing parameters
    6. DWORD tst_gen; //not used
    7. DWORD clk; //not used
    8. DWORD fft_size; //used for data trasferering
    9. }UP_EXCH;
    To copy to clipboard, switch view to plain text mode 
    In the “C”-language notation processing parameters structure PP can by considered as following structure:
    Qt Code:
    1. typedef struct DSPP_tag //DSP processing parameetrs
    2. {
    3. int FFT_Size; //for using in the DSP
    4. int MinDSPSize; // for using in the DSP
    5. int MinDMASize; // for using in the DSP
    6. int DMASizeCnt; // for using in the DSP
    7. int DMASize; // for using in the DSP
    8. float CLCfrq;
    9. int Synch;
    10. int SSht;
    11. int Mode;
    12. int Wch;
    13. int Smd;
    14. int Offt,
    15. int Lb;
    16. int Hb;
    17. int Wb;
    18. int NAvr;
    19. BOOL CAvr;
    20. BOOL Weight;
    21. BOOL DCRem;
    22. BOOL ExtSyn;
    23. BOOL Ch1;
    24. BOOL Ch2;
    25. BOOL ExtWin;
    26. int Clip;
    27. //RF parts
    28. int HPF0;
    29. int HPF1;
    30. int LPF0;
    31. int LPF1;
    32. int ATT0;
    33. int ATT1;
    34. } DSPP;
    To copy to clipboard, switch view to plain text mode 

    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
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2

    Default Re: Read binary from file

    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-...ostcount9.html some structures and read binary file:

    Qt Code:
    1. QDataStream in(&file);
    2. in >> header >> channelA >>channalB>> channelA >>channalB>> channelA >>channalB;
    To copy to clipboard, switch view to plain text mode 
    Last edited by weldpua2008; 1st April 2009 at 16:09.

  3. #3

    Default Re: Read binary from file

    I can read and I use this code:
    Qt Code:
    1. void MdiChild::openFile(QString curFile){
    2. isUntitled = true;
    3. setWindowTitle(curFile + "[*]");
    4. QFile file(curFile);
    5. QApplication::setOverrideCursor(Qt::WaitCursor); //курсор в режим ожидания
    6. if(!file.open(QIODevice::ReadOnly))
    7. {
    8. QMessageBox::warning(this, tr("Spreadsheet"),
    9. tr("Cannot open file %1:\n%2.")
    10. .arg(curFile)
    11. .arg(file.errorString()));
    12. }
    13. else
    14. {
    15. // QDataStream in(&file);
    16. // file_razmer = FileSeek(FH,filec,2);
    17. if (file.read(reinterpret_cast<char*>(&Q.fh), sizeof(Q.fh)) != sizeof(Q.fh))
    18. {
    19. QMessageBox::warning(this, tr("Spreadsheet"),
    20. tr("Cannot read header of file %1:\n%2.")
    21. .arg(curFile)
    22. .arg(file.errorString()));
    23. }
    24. Q.fh2dspp(66000,0,0);
    25. n_vib = (file.size() - (sizeof Q.fh))/(sizeof 4*2*Q.nDSPZ);
    26. QString astr="";
    27. for (i=0;i<16;i++){
    28. astr+= QString::number(i) + ": " + QString::number(Q.fh.syst[i]) + " ";
    29. }
    30. int Naver;
    31. /*if (OF->nAverCB->Checked && n_vib>Naver)
    32.   Naver = StrToInt(OF->nAverEdit->Text);
    33.   else Naver = n_vib;
    34.   */
    35. Naver = n_vib;
    36. QMessageBox::warning(this, tr("Propertis"),
    37. 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 ")
    38. .arg(file.size())
    39. .arg(curFile)
    40. .arg(QString::number(sizeof Q.fh) )
    41. .arg(QString::number(sizeof 4*2*Q.nDSPZ) )
    42. .arg(QString::number(sizeof n_vib) )
    43. .arg(QString::number(sizeof Q.nDSPZ) )
    44. .arg(QString::number(Q.dspp.NAvr) )
    45. .arg(Q.fh.time)
    46. .arg(astr)
    47. .arg(Q.fh.desc)
    48. .arg(QString::number(Naver) )
    49. );
    50. Naver=10;
    51. for(i=0;i<Naver;i++){
    52. file.read(reinterpret_cast<char*>(&Q.spectr), sizeof(4*2*Q.nDSPZ));
    53. //Q.get_spectrAB_prop();
    54. //SeriesShow();
    55. }
    56. }
    57. QApplication::restoreOverrideCursor(); //курсор в нормальный режим
    58. file.close();
    59. }
    To copy to clipboard, switch view to plain text mode 

+ Reply to Thread

Similar Threads

  1. cannot execute binary file
    By mgturner in forum Installation and Deployment
    Replies: 1
    Last Post: 16th March 2009, 17:04
  2. QFile can't read a file
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 20:24
  3. can`t read a binary data!
    By blm in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 16:56
  4. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts