HI. Hopefully I can resolve this now with your help.

simply, I want to write a String to file. I have been trying it with fstream. The string comes in the form of AnsiString from a c++ Edit component. Can someone provide some code to do this.
Since I cannot write a string to disk I have tried converting it to a char[]. The problem wih this is that I dont know how long the string is going to be. If i declare a char[] of say char[256], if i then copy this value back to edit component after reading it back from file. eg Edit1->Text=string[], the edit component gets all 256 parts of the array which then shows up as rubbish if the string was only 6 long in the first place. Ideally I dont want a fixed char array.

Can someone provide some code as I am really frustrated with this.

Regards

NathaN

Assume just String for this case, as I can easily convert between AniString and String

Assume just String for this case, as I can easily convert between AniString and String

And what's a String? Is it like a std::string?

Thanks for that. you got me thinking. This is what the C++ Builder 6 help file says

ShortString 255 characters 2 to 256 bytes backward compatibility
AnsiString ~2^31 characters 4 bytes to 2GB 8-bit (ANSI) characters
WideString ~2^30 characters 4 bytes to 2GB Unicode characters;
multi-user servers and multi-language applications

I have just tries writting/reading a short string and that works fine! finally im getting somewhere. The AnsiString does not want to know.Just get garbage when I write it to disk. WideString does not work either. Looks like im stuck with shortstring. Just have to make sure user does not entr more than 255 characters.

If you can suggest why widestring does not write to disk (in txt or binary) I would be very greatful

>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,

I have been using fstream::write etc. I have gone through the help for c++ builder 6 and cannot find a overload operator for AnsiString. I have tried it and the compiler throws an error saying an operator is not available for ansistring.

I have just tried wfstream. This only writes the first 4 characters to file. And when i try and read back get a access violation. Not sure why this is. Will keep trying to work that one out. At least its now running with ShortString.

As you prob guess I am new to this, and trying to learn c++ the best i can. If you can suggest any online resources for learning this type of stuff then I would be greatful. I usually find learning best if someone can show me how to do it. eg writing string to file. I only need to be shown once and then I will remember it for next time.

Regards
Nath

>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. ;)

This article has been dead for over six months. Start a new discussion instead.