I am not familiar with AnsiString, so I'd recommend reading the documentation your implementation provides. Some Googling suggests that there are methods c_str() and Length() that may be of use.
http://www.functionx.com/bcb/topics/strings.htm
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Assume just String for this case, as I can easily convert between AniString and String
And what's aString? Is it like a std::string?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>Just get garbage when I write it to disk.
You said you were using fstream. Would that be fstream::operator<<, or fstream::write? Since Borland supplies you with AnsiString, it makes sense that they would also provide an overloaded operator<< for it as well. At the very least, IIRC, AnsiString has a direct implicit conversion to const char *. In other words, show us your code.
>If you can suggest why widestring does not write to disk (in txt or binary) I would be very greatful
That's easy, a wide string requires a wide stream to perform the proper character conversions. Try using wfstream rather than fstream.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I have been using fstream::write etc.
A lot of people have been making this mistake recently. A byte-by-byte copy of a non-trivial class will never work. Unless you know what the pitfalls are for unformatted binary I/O, don't use it.
>I have just tried wfstream. This only writes the first 4 characters to file.
That's because you're still probably using write. I can't stress how wrong that is.
>I usually find learning best if someone can show me how to do it. eg writing string to file.
Beginner's rule #1) Stick to the standard and life will be easier. Non-standard libraries may be used as a convenience once you actually know what you're doing. To write a string to a file, you should use the std::string class:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::string s;
std::ofstream out("somefile");
std::cout<<"Enter a string: ";
if (std::getline(std::cin, s))
out<< s <<'\n';
s.clear(); // Clear the string to test input
out.close();
std::ifstream in("somefile");
if (!in)
return EXIT_FAILURE;
std::getline(in, s);
std::cout<< s <<'\n';
return EXIT_SUCCESS;
}
Concerning the Borland string classes, if you don't know why there are so many, you have no business trying to pick which one to use. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401