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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#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");
}
0's and spaces are typically dropped when they are the leading char.

Can you read a single char/byte at a time ?
Last edited on
Topic archived. No new replies allowed.