hi,

i want to make a program simular to this::

http://www.daniweb.com/code/snippet356.html

but i want it to use Strings not char arrays....


PS::In other words i want to binary write a class-struct which contains a string member.....I think the fact that string is of arbitrary size, creates problem...

Recommended Answers

All 6 Replies

>>>>i want to binary write a class-struct which contains a string me
you can't write it out all in one write() function call like you can structures that contain char arrays. You will have to write the members of the class individually.

Here is how to write fixed-length strings, but I don't think this is what you are looking for.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
string string1("Hello");
string string2("World");

cout << setw(10) << string1 << setw(10) << string2 << endl;
cin.ignore();
}

i want to binary write a class-struct which contains a string me
you can't write it out all in one write() function call like you can structures that contain char arrays. You will have to write the members of the class individually.

Here is how to write fixed-length strings, but I don't think this is what you are looking for.

so i can't binary write a string so easily....:sad:

anyway i've been lookin around and i found this site :

http://www.parashift.com/c++-faq-lite/serialization.html#faq-36.6

but it still makes me wander that if we are to use the string class shouldn't more easy to serialize it....:confused:

is there any kind of framework for this kind of job?

c++ makes some things more difficult to do. This is one example -- can't just binary write classes that contain other c++ objects/classes. And C-style structures that contain pointers can not be easily written to binary files either.

And I agree with the article in your link -- there are trade-offs to writing binary files.

plus: writes are fast, easy to create random access fixed-length files

minus: impossible to change the format (such as add or remove data elements) without rewriting the entire file.

if there is only one variable length string in the class we could

#include <cstring>
#include <cstdio>
using namespace std;
struct some_struct
{
  static some_struct* construct const char* s )
  {
    int n = strlen(sz) ;
    some_struct* pss = static_cast<some_struct*>(
          new char[sizeof(some_struct)+n] ) ;
    pss->string_sz = n ;
    strcpy( pss->str, s ) ;
    return pss ;
  }
  static void destroy( some_struct* pss ) { delete[] (char*)pss ; }
  // if required, implement these too
  static some_struct* copy_construct( some_struct* pss ) ;
  static some_struct* assign( some_struct* lvalue, const some_struct* rvalue );

  // now we can perform binary i/o as follows
  void write( FILE* file, const some_struct* object )
  {
     fwrite( &object->string_sz, sizeof(object->string_sz), 1, file ) ;
     fwrite( object, sizeof(some_struct)+object->string_sz, 1, file ) ;
  }
  static some_struct* read( FILE* file )
  {
      int n ;
      fread( &n, sizeof(n), 1, file ) ;
      some_struct* pss = static_cast<some_struct*>( new 
                                                                char[sizeof(some_struct)+n] ) ;
      fread( pss, sizeof(some_struct)+n, 1, file ) ;
      return pss ;
  }

  private:
  // other members
  int string_sz ;
  enum { ANY_SIZE = 1 } ; 
  char str[ANY_SIZE] ; // must be the last instance member var.

  // disable value semantics
  some_struct() {}
  some_struct( const some_struct& ) ;
  void operator= ( const some_struct& ) ;
};

however, this is not a good idea. text output is both more transparent
and more portable. (no surprise that xml, soap etc. are increasingly used.)

thank you all, for your replies...it just makes me sad that none of the "good" books{for starting c++} doesn't state these problems...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.