I'm looking for some help piping a file (16-bit signed little endian integers raw data) from the command line to my program:
cat data.dat | myprogram
Then it should convert the data to 16 bit signed integers. It works for the first 12 values. The 13th value is wrong, followed by zeros.
The second problem is that the program seems to enter the while loop only once.
I am using Windows + MinGW.
My Code:
#include <iostream>
using namespace std;
#define DEFAULT_BUF_LENGTH (16 * 16384)
int main(int argc, char* argv[])
{
char buf[DEFAULT_BUF_LENGTH];
while(cin >> buf)
{
int16_t* data = (int16_t*) buf; //to int
for(int i=0;i<18;i++)
{
cout << data[i] << endl;
}
}
return 0;
}
Output:
0
9621
-14633
-264
5565
-12288
9527
-7109
11710
6351
4096
-5033
5773
147
0
0
0
0
Thanks for your help!
You might try to use read()
instead of the >>
operator, which is generally for formatted input. Also checking how much data was actually read is useful:
#include <iostream>
using namespace std;
#define DEFAULT_BUF_LENGTH (16 * 16384)
int main(int argc, char* argv[])
{
char buf[DEFAULT_BUF_LENGTH];
for(;;)
{
cin.read(buf, sizeof(buf));
int size = cin.gcount();
if (size == 0) break;
int16_t* data = (int16_t*) buf; //to int
for(int i=0;i<size/sizeof(int16_t);i++)
{
cout << hex << data[i] << endl;
}
}
return 0;
}
Statement cin >> buf
does not fill the whole buf
with data. It reads only next "set" of non-whitespace characters.
Change cin >> buf
to read(0, buf, sizeof(buf)) > 0
If you insist on using C++ streams, change beginning of your loop to:
while (!cin.eof()) {
cin.read(buf, sizeof(buf));
[...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With