-
April 13th, 2013, 04:23 PM
#1
[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");
}
-
April 14th, 2013, 07:09 AM
#2
Re: reading bytes without bit loss
You should use unformatted input with binary data, i.e.
"byte = stream.get();" instead of "stream >> byte;".
-
April 14th, 2013, 09:41 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|