Overriding std::fstream << and >> operators

This is a discussion on Overriding std::fstream << and >> operators within the C++ Programming forums, part of the General Programming Boards category; Hi I've overriden the << and >> operators to write data in binary format (instead of text). The function seem ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Question Overriding std::fstream << and >> operators

    Hi
    I've overriden the << and >> operators to write data in binary format (instead of text). The function seem to work ok but I'd like to know if they are sufficient and whether they be improved upon. So here they are. Tthey are members of a class called BinStream that inherits from std::fstream.

    Code:
        template <typename T> BinStream& operator << (const T& input)
        {
            T&& temp= std::move(input);
            this->write(reinterpret_cast<char*>(&temp), sizeof(T));
            return *this;
        }
    
        template <typename T> BinStream& operator >> (T& input)
        {
            this->read(reinterpret_cast<char*>(&input), sizeof(T));
            return *this;
        }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    Well to be honest I am quite skeptical about your code. You could just use the operator<< and operator>> on the class data in your overloaded operators.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    I don't get what you mean actually, could you give an example? Sure it works with class data and it's also handy for writing entire structs from memory to a file.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    Well, not a text file.

    But anyway, the only other thing you have to worry about when making a binary file is endianness, especially if you're going to share data between machines. This makes code more involved since you may have to rearrange data.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Thanks, yes you are right about the endianness and the fact that it's not appropriate for text files. But no problem, I wrote this specifically for a binary file format which uses little endian(just like my PC. So there was no need to rearrange). Ok since there is nothing to report on syntax I'll assume that part is alright.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    whiteflags point is that your code is not portable. There are real-world machines that are not little endian. Your file format is therefore machine dependent.

    More significantly, the output is also highly compiler dependent, for reasons other than endianness. If your type T is a struct or class, the layout in memory (e.g. padding between members) varies between compilers. If any members of that struct/class are a pointer (or reference), then your code will output the pointer (or reference), not the data pointed at. Similarly, if your type T is a class with virtual member functions. That means, if you write your file, and the code reading it was built with a different compiler, the reading program will malfunction.

    If you want to have a useful binary file format, it is a good idea to ensure your file format avoids machine dependence and avoids compiler dependence. You have done neither.

    By convention, the streaming operators are used for formatted text I/O anyway. If you want a fstream that supports binary I/O, it is conventional to override or overload read() and write functions().

    And don't get me started on using std::move() on a const object.
    Last edited by grumpy; 03-11-2012 at 03:55 PM.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by grumpy View Post
    And don't get me started on using std::move() on a const object.
    What is the problem (as long as temp isn't returned) ?
    (..though I don't know what the logic behind doing it just for a cast statement is ..)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    The problem is the same as casting away constness. It's just wrong and can lead to strange behavior.
    write shouldn't modify its argument, so taking const is fine. Otherwise, if you really want an r-value reference, then just declare it so!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  2. Overriding
    By vb.bajpai in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 04:28 AM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. Overriding '+' operator
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2005, 03:24 PM
  5. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21