I need to read 16 bits from the binary file as std::string or char *. For example, a binary file contains 89 ab cd ef, and I want to be able to extract them as std::strings or char *. I have tried the following code:
ifstream *p = new ifstream();
char *buffer;
p->seekg(address, ios::beg);
buffer = new char[16];
memset(buffer, 0, 16);
p->read(buffer, 16);
When I try to std::cout the buffer, nothing appeared. How can I read these characters in the binary file?
EDIT: I was looking for the buffer to be a int type such as "0x89abcdef". Is it possible to achieve?
Something like:
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
int main()
{
if (ifstream input("filename"))
{
std::string s(2 /*bytes*/, '\0' /*initial content - irrelevant*/);
if (input.read(&s[0], 2 /*bytes*/))
std::cout << "SUCCESS: [0] " << std::hex << (int)s[0] << " [1] " << (int)s[1] << '\n';
else
std::cerr << "Couldn't read from file\n";
}
else
std::cerr << "Couldn't open file\n";
}
You can't read a binary stream as though it were text.
You can, of course, read as binary (by using "file.read()" and "file.write()" methods on your stream object). Just like what you're doing now :)
You can also convert binary to text: "convert to hex text string" and "uuencode base 64" are two common ways to do this.
You'll want to read the bytes as numbers (of type long long probably).
Then you can print those using formatting specifiers like this:
#include <iostream>
#include <iomanip>
int main()
{
using namespace std;
int x = 2;
int y = 255;
cout << showbase // show the 0x prefix
<< internal // fill between the prefix and the number
<< setfill('0'); // fill with 0s
cout << hex << setw(4) << x << dec << " = " << setw(3) << x << endl;
cout << hex << setw(4) << y << dec << " = " << setw(3) << y << endl;
return 0;
}
Related
I'm trying to parse .wav files in C++.
The 44 bytes in the header of the .wav file are some of the file's meta information, which I am trying to parse.
I parsed it in Python and got the following, which should be correct
Chunk_id : RIFF
Chunk_size : 468556
Format : WAVE
fmt_id : fmt
fmt_size : 16
audio_format : 1
channels_count : 1
sample_rate : 44100
byte_rate : 88200
block_align : 2
bits_per_sample : 16
data_id : data
data_size : 468520
But when I switch in to C++, I got this:
ChunkID: RIFFL&
ChunkSize: 468556
Format: WAVEfmt
FmtID: fmt
FmtChunkSize: 16
FmtAudioFormat: 1
FmtChannelNumber: 1
FmtSampleRate: 44100
FmtByteRate: 88200
FmtBlockAlign: 2
FmtBitPerSample: 16
DataChunkID: data(&
The problem is three fields consisting of a char array of four bytes.
ChunkID: RIFFL&, Format: WAVEfmt, DataChunkID: data(&
As parsed by Python, the contents of the three fields should be RIFF, WAVE, data.
And this is my C++ code.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct WaveChunk
{
char ChunkID[4];
int ChunkSize;
char Format[4];
// fmt
char FmtID[4];
int FmtChunkSize;
short FmtAudioFormat;
short FmtChannelNumber;
int FmtSampleRate;
int FmtByteRate;
short FmtBlockAlign;
short FmtBitPerSample;
// fmt
// data
char DataChunkID[4];
int DataChunkSize;
// data
};
string WaveChunkToString(WaveChunk* wavechunk){
stringstream ss;
ss << "ChunkID: " << wavechunk->ChunkID << "\n";
ss << "ChunkSize: " << wavechunk->ChunkSize << "\n";
ss << "Format: " << wavechunk->Format << "\n";
ss << "FmtID: " << wavechunk->FmtID << "\n";
ss << "FmtChunkSize: " << wavechunk->FmtChunkSize << "\n";
ss << "FmtAudioFormat: " << wavechunk->FmtAudioFormat << "\n";
ss << "FmtChannelNumber: " << wavechunk->FmtChannelNumber << "\n";
ss << "FmtSampleRate: " << wavechunk->FmtSampleRate << "\n";
ss << "FmtByteRate: " << wavechunk->FmtByteRate << "\n";
ss << "FmtBlockAlign: " << wavechunk->FmtBlockAlign << "\n";
ss << "FmtBitPerSample: " << wavechunk->FmtBitPerSample << "\n";
ss << "DataChunkID: " << wavechunk->DataChunkID << "\n";
ss << "DataChunkSize: " << wavechunk->DataChunkSize << endl;
return ss.str();
}
using namespace std;
int main(){
WaveChunk w;
ifstream inf("target.wav", ios::binary|ios::in);
inf.read((char* ) &w, sizeof(WaveChunk));
cout << WaveChunkToString(&w);
return 0;
}
That's it, why are these three fields parsed differently than expected, but the other fields made up of numbers are fine?
In this line and the other lines to print char something[4];:
ss << "ChunkID: " << wavechunk->ChunkID << "\n";
std::stringstream will read until null-character '\0' but the array doesn't contain that, so it will read beyond the allocated buffer.
You will have to specify the size to print for properly printing the array without adding extra byte.
It can be done like this:
ss << "ChunkID: "; ss.write(wavechunk->ChunkID, 4); ss << "\n";
Why can't I use "fixed" and "setprecision()" with the +operator to format it into a string and that I only can use it only with the less-than-less-than-operator to format it for cout. What are other ways I can implement this?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a = 157.2734;
cout << "This number is " << fixed << setprecision(1) << a << "." << endl;
string line = "This number is " + fixed + setprecision(1) + a + "." + "\n"; // This doesn't work this way! Why!?!?!?
cout << line;
return 0;
}
Why can't I use "fixed" and "setprecision()" with the +operator to format it into a string?
Have a closer look at std::fixed and std::setprecision().
The complete signature of std::fixed:
std::ios_base& fixed(std::ios_base& str);
So, it is designed to work with streams exclusively.
In the case of std::setprecision(), it's a bit tricky:
/*unspecified*/ setprecision( int n );
However:
Returns an object of unspecified type such that if str is the name of an output stream of type std::basic_ostream or an input stream of type std::basic_istream, then the expression str << setprecision(n) or str >> setprecision(n) behaves as if the following code was executed:
str.precision(n);
So, it might have worked if there were a std::string::precision() method but there isn't one.
What are other ways I can implement this?
The possible solution:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
double a = 157.2734;
cout << "This number is " << fixed << setprecision(1) << a << "." << endl;
ostringstream fmtStr;
fmtStr << "This number is " << fixed << setprecision(1) << a << ".\n";
string line = fmtStr.str();
cout << line;
return 0;
}
Output:
This number is 157.3.
This number is 157.3.
Life demo on ideone
I am new in C++ in general, and thus, also in file handling in C++.
I need to read a .raw file which has 16-bit integers, and have dimension 512 x 512.
For that I am using following code:
ifstream myData("myData.raw");
short value;
int i = 0;
char buf[sizeof(short)];
while (myData.read(buf,sizeof(buf)))
{
memcpy(&value, buf, sizeof(value));
cout << value << " ";
i++;
}
cout << endl << "Total count: " << i << endl;
The value i am getting for i is not 512 x 512. So I guess something is not right.
Can someone please help me in this regard?
The default open mode is "text" and some characters will be possibly dropped or treated as end of file. ios::binary stops these alterations.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream myData("myData.raw", ios::binary);
short value;
int i = 0;
char buf[sizeof(short)];
while (myData.read(buf, sizeof(buf)))
{
memcpy(&value, buf, sizeof(value));
cout << value << " ";
i++;
}
cout << endl << "Total count: " << i << endl;
}
Is there a way to make setw and setfill pad the end of a string instead of the front?
I have a situation where I'm printing something like this.
CONSTANT TEXT variablesizeName1 .....:number1
CONSTANT TEXT varsizeName2 ..........:number2
I want to add a variable amount of '.' to the end of
"CONSTANT TEXT variablesizeName#" so I can make ":number#" line up on the screen.
Note: I have an array of "variablesizeName#" so I know the widest case.
Or
Should I do it manually by setting setw like this
for( int x= 0; x < ARRAYSIZE; x++)
{
string temp = string("CONSTANT TEXT ")+variabletext[x];
cout << temp;
cout << setw(MAXWIDTH - temp.length) << setfill('.') <<":";
cout << Number<<"\n";
}
I guess this would do the job but it feels kind of clunky.
Ideas?
You can use manipulators std::left, std::right, and std::internal to choose where the fill characters go.
For your specific case, something like this could do:
#include <iostream>
#include <iomanip>
#include <string>
const char* C_TEXT = "Constant text ";
const size_t MAXWIDTH = 10;
void print(const std::string& var_text, int num)
{
std::cout << C_TEXT
// align output to left, fill goes to right
<< std::left << std::setw(MAXWIDTH) << std::setfill('.')
<< var_text << ": " << num << '\n';
}
int main()
{
print("1234567890", 42);
print("12345", 101);
}
Output:
Constant text 1234567890: 42
Constant text 12345.....: 101
EDIT:
As mentioned in the link, std::internal works only with integer, floating point and monetary output. For example with negative integers, it'll insert fill characters between negative sign and left-most digit.
This:
int32_t i = -1;
std::cout << std::internal
<< std::setfill('0')
<< std::setw(11) // max 10 digits + negative sign
<< i << '\n';
i = -123;
std::cout << std::internal
<< std::setfill('0')
<< std::setw(11)
<< i;
will output
-0000000001
-0000000123
Something like:
cout << left << setw(MAXWIDTH) << setfill('.') << temp << ':' << Number << endl;
Produces something like:
derp..........................:234
herpderpborp..................:12345678
#include <iostream>
#include <iomanip>
int main()
{
std::cout
<< std::setiosflags(std::ios::left) // left align this section
<< std::setw(30) // within a max of 30 characters
<< std::setfill('.') // fill with .
<< "Hello World!"
<< "\n";
}
//Output:
Hello World!..................
I've been trying to format the output to the console for the longest time and nothing is really happening. I've been trying to use as much of iomanip as I can and the ofstream& out functions.
void list::displayByName(ostream& out) const
{
node *current_node = headByName;
// I have these outside the loop so I don't write it every time.
out << "Name\t\t" << "\tLocation" << "\tRating " << "Acre" << endl;
out << "----\t\t" << "\t--------" << "\t------ " << "----" << endl;
while (current_node)
{
out << current_node->item.getName() // Equivalent tabs don't work?
<< current_node->item.getLocation()
<< current_node->item.getAcres()
<< current_node->item.getRating()
<< endl;
current_node = current_node->nextByName;
}
// The equivalent tabs do not work because I am writing names,
// each of different length to the console. That explains why they
// are not all evenly spaced apart.
}
Is their anything that I can use to get it all properly aligned with each other?
The functions that I'm calling are self-explanatory and all of different lengths, so that don't align very well with each other.
I've tried just about everything in iomanip.
Think of it like using Microsoft Excel :)
You think of your stream as fields. So you set the width of the field first then you insert your text in that field. For example:
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
using namespace std;
string firstName = "firstName",
secondName = "SecondName",
n = "Just stupid Text";
size_t fieldWidth = n.size(); // length of longest text
cout << setw(fieldWidth) << left << firstName << endl // left padding
<< setw(fieldWidth) << left << secondName << endl
<< setw(fieldWidth) << left << n << endl;
cout << setw(fieldWidth) << right << firstName << endl // right padding
<< setw(fieldWidth) << right << secondName << endl
<< setw(fieldWidth) << right << n << endl;
}
......
......
The field width means nothing but the width of the text + spaces. You could fill anything other than spaces:
string name = "My first name";
cout << setfill('_') << setw(name.size() + 10) << left << name;
.....
output::
My first name__________
......
I think the best way is to figure out your format then, write a new formatter that does all what you want:
#include <iostream>
#include <iomanip>
#include <string>
std::ostream& field(std::ostream& o)
{
// usually the console is 80-character wide.
// divide the line into four fields.
return o << std::setw(20) << std::right;
}
int main()
{
using namespace std;
string firstName = "firstName",
secondName = "SecondName",
n = "Just stupid Text";
size_t fieldWidth = n.size();
cout << field << firstName << endl
<< field << secondName << endl
<< field << n << endl;
}
If you started thinking about parametrized manipulators, only that accept one int or long parameter are easy to implement, other types are really obscure if you are not familiar with streams in C++.
Boost has a format library that allows you to easily format the ourput like the old C printf() but with type safety of C++.
Remember that the old C printf() allowed you to specify a field width. This space fills the field if the output is undersized (note it does not cope with over-sized fields).
#include <iostream>
#include <iomanip>
#include <boost/format.hpp>
struct X
{ // this structure reverse engineered from
// example provided by 'Mikael Jansson' in order to make this a running example
char* name;
double mean;
int sample_count;
};
int main()
{
X stats[] = {{"Plop",5.6,2}};
// nonsense output, just to exemplify
// stdio version
fprintf(stderr, "at %p/%s: mean value %.3f of %4d samples\n",
stats, stats->name, stats->mean, stats->sample_count);
// iostream
std::cerr << "at " << (void*)stats << "/" << stats->name
<< ": mean value " << std::fixed << std::setprecision(3) << stats->mean
<< " of " << std::setw(4) << std::setfill(' ') << stats->sample_count
<< " samples\n";
// iostream with boost::format
std::cerr << boost::format("at %p/%s: mean value %.3f of %4d samples\n")
% stats % stats->name % stats->mean % stats->sample_count;
}
Give up on the tabs. You should be able to use io manipulators to set the field width, the fill character, and the format flag (to get left or right justification). Use the same values for the headings as you do for the data, and everything should come out nicely.
Also beware that you've switched Rating and Acres in your example.
You can write a procedure that always print the same number of characters to standard output.
Something like:
string StringPadding(string original, size_t charCount)
{
original.resize(charCount, ' ');
return original;
}
And then use like this in your program:
void list::displayByName(ostream& out) const
{
node *current_node = headByName;
out << StringPadding("Name", 30)
<< StringPadding("Location", 10)
<< StringPadding("Rating", 10)
<< StringPadding("Acre", 10) << endl;
out << StringPadding("----", 30)
<< StringPadding("--------", 10)
<< StringPadding("------", 10)
<< StringPadding("----", 10) << endl;
while ( current_node)
{
out << StringPadding(current_node->item.getName(), 30)
<< StringPadding(current_node->item.getLocation(), 10)
<< StringPadding(current_node->item.getRating(), 10)
<< StringPadding(current_node->item.getAcres(), 10)
<< endl;
current_node = current_node->nextByName;
}
}