Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:07:38+00:00 2026-05-26T13:07:38+00:00

Is there anything particularly bad or naive about reading serialized data (of a known

  • 0

Is there anything particularly bad or naive about reading serialized data (of a known format + endianness) from a file using something like this? I’m not worried about portability and realistically it would only be used by me. I know one issue would be attempting to extract to a non-packed POD struct, although I can always define a separate operator> for each such struct.

template<typename T> inline std::fstream& operator> (std::fstream& fs, T& i) {
    static_assert(std::is_pod<T>::value, "Not POD");
    fs.read(reinterpret_cast<char*>(&i), sizeof i);
    return fs;
}
template<typename T> inline std::fstream& operator> (std::fstream& fs, std::vector<T>& v) {
    static_assert(std::is_pod<T>::value, "Not POD");
    fs.read(reinterpret_cast<char*>(&v[0]), sizeof(T) * v.size());
    return fs;
}
template<typename T> inline std::fstream& operator> (std::fstream& fs, std::vector<std::vector<T>>& v) {
    for (auto& i : v)
        fs > i;
    return fs;
}
inline std::fstream& operator> (std::fstream& fs, std::string& s) {     
    fs.read(reinterpret_cast<char*>(&s[0]), s.size());
    return fs;
}

std::fstream f("file", std::ifstream::in | std::ifstream::out | std::ifstream::binary);
int i;
char j;
std::vector<std::vector<char>> vec(5, std::vector<char>(8));

f > i > j > vec;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:07:39+00:00Added an answer on May 26, 2026 at 1:07 pm

    I’m not sure what you’re asking. The code you post doesn’t read
    serialized data “of a known format or endianness”. It copies bytes from
    the file to where ever, which could result in undefined behavior, and is
    very unlikely to result in the correct values unless by some weird bit
    of luck, the “known format” corresponds exactly to that used internally
    on your machine. I a lot of cases, in fact, it will probably cause the
    program to crash; think of what might happen if the structure you’re
    trying to read contains a pointer, for example.

    I might add that overloading ‘>’ for this is horrible overload abuse; if
    you want to read a special format, the logical way to go about it is to
    define a corresponding class (ixdrstream, for example), and overload
    ‘>>’ for it. (ixdrstream will probably derive from
    std::basic_ios<char>, of course, and will almost certainly use the
    standard streambuf for actual input.)

    And of course, you never overload on std::fstream, but rather on
    std::istream (or std::ostream for output). (FWIW, I don’t think
    I’ve ever used an std::fstream. The stream idiom really doesn’t
    support mixing reads and writes elegantly.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anything particularly bad about doing something like this in Javascript: myapp.someObject =
I'm using awesome_nested_set. And there are of course many JS libraries. Anything particularly good
Is there anything wrong or inherently unsafe about the way I've programmed this? I'm
Is there anything similar to the Spring JMX exporter out there WITHOUT using the
Is there anything similar to jFormer (particularly the survey form) available for ASP.Net? I
I'm attempting to read a file (particularly /proc/stat) to get data out of it.
There are many systems that depend on the uniqueness of some particular value. Anything
is there anything more comfortable than this to detect a special page? The page
Is there anything wrong with this code or can this be done more efficiently?
Is there anything like asp.net transactions in Android SQLite? I am doing some processing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.