Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MagTDK

Saving STL string to file

This topic is 5984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I save a stl string to file so that it conforms to a structure like this:
    
struct Book
{
  int address;
  char firstname[32]; // String can be less, but no more then 32 characters

  char lastname[32];
  int zipcode;
}
    
The first and last name are the stl strings I want to save in the structure above. [edited by - MagTDK on June 30, 2002 5:21:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by MagTDK
The first and last name are the stl strings I want to save in the structure above.



A char[] is not a STL string. A std::string is.

A STL string would be saved simply by using the << operator on a file stream :


  
#include <fstream>

#include <string>

using namespace std;

ofstream ofs( "output.txt" );
string str;

ofs << str;


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites

What I meant to say was some where else in my code I''m using the first and last names as STL strings and not char[]. But now I need to convert them over into a structure similar to the one I have posted using char[] type (this of course is a example and not the real structure). Also note, it has to be binary and not a text file. From there, another C++ library will extract the info I saved to disk.

Is there a way to do it using strictly C++ or STL and not C style functions?

Share this post


Link to post
Share on other sites
You could do something to the effect of :


    
template<class T>
ostream& BinWrite( ostream& os, const T& data )
{
os.write( (char*)&data, sizeof(T) );
return os;
};

template<>
ostream& BinWrite<std::string>( ostream& os, const std::string& data )
{
os.write( data.c_str(), data.length() );
os.write( "\0", 1 );
return os;
};

template
ostream& BinWrite<Book>( ostream& os, const Book& data )
{
BinWrite(os, data.address);
BinWrite(os, firstname ); // as a std::string !

BinWrite(os, lastname ); // as a std::string !

BinWrite(os, zipcode );
};


Using type traits to distinguish between POD and non-POD may actually be safer, because here, if the template specialisation isn't defined, a simple bit-blast will be done.

Read up on that

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 30, 2002 8:35:41 PM]

Share this post


Link to post
Share on other sites

std::string fname("John"), lname("Rambo");
Book bk;
...
strncpy(bk.firstname, fname.c_str(), 32);
strncpy(bk.lastname, lname.c_str(), 32);
// be careful with the above because
// it MIGHT NOT contain ''\0''
// OR
// ->strncpy(bk.firstname, name.c_str(), 31);
// ->bk.firstname[31] = 0;
// if you need your char array to be ''\0''-terminated.
...

Hope that helps

Share this post


Link to post
Share on other sites
Open an fstream to the file you want to write to in binary mode.
Retrieve the char* associated with the string by using string.c_str(). You then find the length of this c-string(or you could probably just call string.size() for it), if its less than 32 you do fstream.write(string.c_str(), length)
If its 32 or more you do fstream.write(string.c_str(), 32).

[of course you should never have numbers in directly in your code, always declare constants(like const MAX_NAME_LENGTH = 32)]

EDIT: Also remember to pay heed to the null-terminating stuff, as mentioned above.

[edited by - ziphnor on July 1, 2002 4:05:45 AM]

Share this post


Link to post
Share on other sites
quote:

C++ std::strings do not care about NULLs.



Okay, so if he wants null-terminated c style strings in his struct after loading he will have add the /0 himself.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!