fstream file;

In writing into a .dat file what does the following line mean?

file.write((char *) this, sizeof(PasswordGenerator));

And how can i convert this bit of code if i want to write it into a .txt file?

3 Years
Discussion Span
Last Post by Duoas

That code is wrong in a number of subtle and dangerous ways. But what it is trying to do is write the binary value of a 'PasswordGenerator' object to file.

Any object you wish to write should have a function to explicitly convert it to either text or binary. If you google around "serialization" you'll find all kinds of stuff, but it might be overload.

Here's a simple example to get you going:

// Here's our fictional object type.
// It contains an integer (very easy to write to file)
// and a string (not so easy).
class Quux
    int         x;
    std::string s;

    Quux( int x, const std::string& s ): x(x), s(s) { }

  // The stream insertion operator must be a friend
  friend std::ostream& operator << ( std::ostream&, const Quux& );

  // As must be the stream extraction if you intend to read a Quux from stream
  friend std::istream& operator >> ( std::istream&, Quux& );

// Here's a function to turn a string like [Hello "Bill"] into [Hello \"Bill\"]
std::string quote( const std::string& s )
  std::string result;
  for (char c: s)
    switch (c)
      case '\\': s += "\\\\";
      case '\"': s += "\\"";
      // You can add any other transformation you like here. For example, 
      // you can turn newlines into [\n] with  case '\n': s += "\\n";
      default:   s += c;
  return result;

// Here's the stream insertion operator function, which writes a string that looks like
//   quux x=72 s="Hello \"Bill\"
// Notice that it doesn't add a newline or anything.
std::ostream& operator << ( std::ostream& outs, const Quux& quux )
  return outs << "quux x=" << quux.x << " s=\" << quote( quux.s ) << "\"";

// Likewise, here's our stream extraction operator function.
// Notice that it is A LOT more complicated than the stream insertion operator. Alas.
std::istream& operator >> (std::istream& ins, Quux& quux )
  std::string s;
  ins >> s;
  if (s != "quux") 
    // Fooey! That's not a Quux!
    ins.setstate( std::ios::failbit );
    return ins;

  char c, e, p;
  ins >> c >> e;
  if ((c != 'x') || (e != '='))
    // Fooey! The Quux is misformatted!
    ins.setstate( std::ios::failbit );
    return ins;
  ins >> quux.x;

  ins >> c >> e >> p;
  if ((c != 's') || (e != '=') || (p != '"'))
    // Fooey! The Quux is misformatted!
    ins.setstate( std::ios::failbit );
    return ins;

  // Make sure to clear any previous value
  quux.s = "";

  // And we'll work by getting everything to the next "
  // If the next " is not immediately after a \ then
  // we'll add the " to the end of the string and continue
  // the loop.
  s = "";
  while (std::getline( ins, s, '"' ))
    quux.s += s;
    if (s.empty() || (quux.s.back() != '\\'))
    quux.s += '"';

  // Don't forget to fix the string!
  // (Writing this routine is left as an exercise for the reader.)
  quux.s = unquote( quux.s );

  // And all stream operators must return their stream argument.
  return ins;

Well, I forgot I'd have to type so much. I haven't tested the above code -- it is possible I made a stupid mistake. (With my luck, probable even.)

But hopefully it should help you get started with basic serialization.

If you are up to it, Boost has a serialization library which is very nice.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.