CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2013
    Posts
    2

    [RESOLVED] reading bytes without bit loss

    Hi,
    I need to read repeatedly data from a MPEG2 file to the buffer of 188 bytes and analyse data bit by bit.

    I have the problem with correct bytes reading from file. In my code listed below I have two methods for that.

    First one is lossing this bytes which in hex_base mode have 0 at the begining, eg: 03, 0F, etc.

    The second method based on read function which need to have buffer as a char (lenght > 1 byte). Because of that I receive different values from that from file in some cases.

    How can I properly read such file? Any ideas?

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
     fstream plik_in;
    
     const int TS_LEN=188;
    
     //bufor do pierwszej metody
     byte buffor[TS_LEN];
     byte bajt;
    
    //bufor do drugiej metody
    char buffor2[TS_LEN];
    
    //plik do obrobienia
    char path1[40]= "TEST2.ts";
    
    //otwieram plik
    plik_in.open( path1 ,ios::in|ios::binary/*|ios::dec,|ios_base::showbase*/);
     
     if(!plik_in)
     {
      cout << "Problem z plikiem wsadowym\n";
     }
    
     // 1 METHOD
     for(int i=0;i<TS_LEN;++i)
     {
    	 plik_in >> bajt;
    	 buffor[i]=bajt;
    	 cout << "buffor[" << i <<"]: " << hex << (int)buffor[i] << endl;
     } 
     plik_in.close(); 
    
    
      // 2 METHOD
     //otwieram plik
    plik_in.open( path1 ,ios::in|ios::binary/*|ios::dec,|ios_base::showbase*/);
    std::cout << "Reading " << TS_LEN << " characters...\n ";
    
        // read data as a block:
        plik_in.read (buffor2,TS_LEN);
    
    	for(int i=0;i<TS_LEN;++i)
    	 {
    		 cout << "buffor[" << i <<"]: " << hex << (int)buffor[i] << endl;
    	 } 
    
    plik_in.close();
    
    system("PAUSE");
    }

  2. #2
    Join Date
    Feb 2004
    Location
    Helsinki, Finland
    Posts
    31

    Re: reading bytes without bit loss

    You should use unformatted input with binary data, i.e.
    "byte = stream.get();" instead of "stream >> byte;".

  3. #3
    Join Date
    Apr 2013
    Posts
    2

    Re: reading bytes without bit loss

    Thank's for that-it works . Your answer is very helpful.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured