Hello,
I am trying to take advantage of the formatted ouput operator of
ostream to rewrite this piece of code /more nicely/:
oftream fp("/tmp/foo", std::ios::out | std::ios::binary);
uint32_t v = 38;
void* ptr = &v;
fp.write ( (char*)ptr,(size_t)4 );
Unfortunately I cannot find a way to use '<<'.
Any idea ?
Thanks
Mathieu 4 1539
"Mathieu Malaterre" <ma*******@Mfree.fr> wrote... I am trying to take advantage of the formatted ouput operator of ostream to rewrite this piece of code /more nicely/:
oftream fp("/tmp/foo", std::ios::out | std::ios::binary); uint32_t v = 38; void* ptr = &v; fp.write ( (char*)ptr,(size_t)4 );
Unfortunately I cannot find a way to use '<<'.
Any idea ?
What's your intent? The code as written outputs four bytes
that represent your 'uint32_t' value. Each byte is output without
change, without formatting. That's _binary_ output. Operator <<
is _formatted_ output. They are generally not interchangeable.
If you opened the stream for binary output, use binary output means.
V
Victor Bazarov wrote: "Mathieu Malaterre" <ma*******@Mfree.fr> wrote...
I am trying to take advantage of the formatted ouput operator of ostream to rewrite this piece of code /more nicely/:
oftream fp("/tmp/foo", std::ios::out | std::ios::binary); uint32_t v = 38; void* ptr = &v; fp.write ( (char*)ptr,(size_t)4 );
Unfortunately I cannot find a way to use '<<'.
Any idea ?
What's your intent? The code as written outputs four bytes that represent your 'uint32_t' value. Each byte is output without change, without formatting. That's _binary_ output. Operator << is _formatted_ output. They are generally not interchangeable. If you opened the stream for binary output, use binary output means.
Alright,
I wan't clear, let's rephrase this. The previous code is clearly
unreadable and any decent compiler should refuse to compile such an ugly
code. I am trying to find a way to rewrite those 3 ugly lines in a more
c++ like approach.
My first attempt was:
ostream& operator<<( ostream& o, const uint32_t& f )
{
void *p = &f;
o.write( (char*) p, sizeof(4) );
}
But this create conflict when used, I am not sure why. Does this mean I
should rewrite a wrapper around ostream/fstream to handle: uint32_t,
uint16_t and uint8_t.
Thanks
Mathieu
"Mathieu Malaterre" <ma*******@Mfree.fr> wrote... Victor Bazarov wrote: "Mathieu Malaterre" <ma*******@Mfree.fr> wrote...
I am trying to take advantage of the formatted ouput operator of ostream to rewrite this piece of code /more nicely/:
oftream fp("/tmp/foo", std::ios::out | std::ios::binary); uint32_t v = 38; void* ptr = &v; fp.write ( (char*)ptr,(size_t)4 );
Unfortunately I cannot find a way to use '<<'.
Any idea ?
What's your intent? The code as written outputs four bytes that represent your 'uint32_t' value. Each byte is output without change, without formatting. That's _binary_ output. Operator << is _formatted_ output. They are generally not interchangeable. If you opened the stream for binary output, use binary output means.
Alright,
I wan't clear, let's rephrase this. The previous code is clearly unreadable and any decent compiler should refuse to compile such an ugly code. I am trying to find a way to rewrite those 3 ugly lines in a more c++ like approach. My first attempt was:
ostream& operator<<( ostream& o, const uint32_t& f ) { void *p = &f; o.write( (char*) p, sizeof(4) ); }
But this create conflict when used, I am not sure why.
Probably because your 'uint32_t' is one of the fundamental types for
which there is already the output operator defined.
Does this mean I should rewrite a wrapper around ostream/fstream to handle: uint32_t, uint16_t and uint8_t.
I don't think so.
I think, Mathieu, you're a bit confused about what is and what isn't "C++".
Once again, operator<< and operator>> as far as streams are concerned are
_formatted_ output and input, respectively. Making them _non-formatted_
is like overloading ++ and make it decrement. Is there a point to this?
V
"Mathieu Malaterre" <ma*******@Mfree.fr> wrote in message
news:t9*******************@twister.nyroc.rr.com... Victor Bazarov wrote: "Mathieu Malaterre" <ma*******@Mfree.fr> wrote...
I am trying to take advantage of the formatted ouput operator of ostream to rewrite this piece of code /more nicely/:
oftream fp("/tmp/foo", std::ios::out | std::ios::binary); uint32_t v = 38; void* ptr = &v; fp.write ( (char*)ptr,(size_t)4 );
Unfortunately I cannot find a way to use '<<'.
Any idea ?
What's your intent? The code as written outputs four bytes that represent your 'uint32_t' value. Each byte is output without change, without formatting. That's _binary_ output. Operator << is _formatted_ output. They are generally not interchangeable. If you opened the stream for binary output, use binary output means.
Alright,
I wan't clear, let's rephrase this. The previous code is clearly unreadable and any decent compiler should refuse to compile such an ugly code. I am trying to find a way to rewrite those 3 ugly lines in a more c++ like approach. My first attempt was:
ostream& operator<<( ostream& o, const uint32_t& f ) { void *p = &f; o.write( (char*) p, sizeof(4) ); }
But this create conflict when used, I am not sure why. Does this mean I should rewrite a wrapper around ostream/fstream to handle: uint32_t, uint16_t and uint8_t.
Thanks Mathieu
Here's 'more C++' like approach using templates.
template <class T>
ostream& binary_write(ostream& os, const T& val)
{
return os.write(reinterpret_cast<const char*>(&val), sizeof val);
}
template <>
ostream& binary_write(ostream& os, const char* val)
{
return os.write(val, strlen(val));
}
ofstream fp("/tmp/foo", std::ios::out | std::ios::binary);
binary_write(fp, 38);
binary_write(fp, 42);
double x = 1.234;
binary_write(fp, x);
But this is a slight risky since you can use it with non-POD types which
would get you into trouble. E.g.
string y = "abc";
binary_write(fp, y);
That would compile but not do anything sensible.
I agree with Victor's comments about <<, you shouldn't be trying to overload
that for binary output, that is just too confusing. I would stick with
functions.
All code is untested.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David Briggs |
last post by:
I am using MS VC++ 6.0 with MFC
I have a simple class:
#include <fstream.h>
class Data
{
public:
CString WriteStr();
Data();
|
by: Ben |
last post by:
I am having trouble calling functions in another file.. Lets look at
the scenario below:
File xyz.cc: (I have both xyz.h and xyz.cc, for simplicity I've
avoided xyz.h)
------------
struct xyz...
|
by: ratzeel |
last post by:
The following snippet code throws an error while compiling on SUN os..
Any idea how to resolve this...
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <algorithm>...
|
by: NewToCPP |
last post by:
I am having some trouble including "iostream" "fstream" to my code.
I tried the following ways and base times it did not work. What is the
problem?
test.cc:
=======
.....
#include...
|
by: MForey |
last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
|
by: ehui928 |
last post by:
The following program is used to open a file in mode both read and
append, but it has
a problem that the file can't be opened.
#include <iostream>
#include <fstream>
#include <cstdlib>
...
|
by: wiso |
last post by:
My problem is this (from:
http://www.cplusplus.com/ref/iostream/fstream/open.html)
#include <fstream>
using namespace std;
int main()
{
fstream f;
|
by: Soneji |
last post by:
*sigh* Ok, I have two questions.
First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2;
I recently started trying to use 'fstream' to work my input/output, and I noticed that using
the...
|
by: Gaijinco |
last post by:
Should this do something?
#include <fstream>
#include <string>
int main()
{
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
std::string s="";
|
by: HaLo2FrEeEk |
last post by:
I've got a program that downloads a set of thumbnail images specified in a remote XML file. There can be up to 30 thumbnails downloaded. I'm using a foreach loop iterating through an XmlNodeList of...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |