struct Book
{
int address;
char firstname[32]; // String can be less, but no more then 32 characters
char lastname[32];
int zipcode;
}
Saving STL string to file
How do I save a stl string to file so that it conforms to a structure like this:
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]
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 ]
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?
You could do something to the effect of :
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]
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;};templateostream& 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]
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
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]
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]
quote:Original post by Ziphnor
EDIT: Also remember to pay heed to the null-terminating stuff, as mentioned above.
C++ std::strings do not care about NULLs.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement