Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ Strings
#1
I hate them so much. ".ToString()" was nice and simple in C#. C++ makes everything hard and gives me errors.

Code:
ofstream file;
             file.open ("output.txt");
             file << Output->Text;
             file.close();

Here I am trying to save everything from a textbox (Output) to a *.txt file, but I get an error about converting between types. How do I make this work?
Reply
#2
I'm not a C++ programmer, but I can guess.

c_str is your friend.

The traditional C++ string is a Pascal string in that it has no null terminator. If you want the null terminator, you have to call the c_str method.
Reply
#3
"<<" should be able to output a string to a file. What type is Output->Text?
Reply
#4
Just a normal textbox. "System.Windows.Forms.TextBox"
Code:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
Reply
#5
Post everything from the method, please.
Reply
#6
Code:
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
         {
             ofstream file;
             file.open ("output.txt");
             file << Output->Text;
             file.close();
         }

?
Outside of that is just a bunch of stuff that handles the other buttons, textchange event in Output, et cetera.
Reply
#7
Edit: Oh this is Visual C++.

I wouldn't know, but try something in here
http://msdn.microsoft.com/en-us/library/...thods.aspx
Reply
#8
This is C++/CLI, not C++.

A CLR string is not a C++ std:Confusedtring. You need to convert between them. Here's some code if you don't care that it only works for ASCII (which I assume is the case since you're using a std:Confusedtring).

Code:
//Convert the CLR string to an array of bytes using the ASCII encoding
array<Byte> ^outputChars = System::Text::Encoding::ASCII->GetBytes(Output->Text);

// Pin the array so the garbage collector can't move it while you're using raw pointers to access it. Might crash if Output->Text is empty, you should add a check for that
pin_ptr<Byte> outputCharsPointer = &(outputChars[0]);

// convert the pin_ptr<Byte> to a char* (NOT a C-string because it's not null-terminated!)
char *nativeCharsPointer = reinterpret_cast<char *>(static_cast<unsigned char *>(outputCharsPointer));

// Convert the char* to a std::string
std::string outputStdString(nativeCharsPointer, outputChars->Length);

But why use C++ iostreams when you can use .NET streams?
Reply
#9
Spaz Wrote:But why use C++ iostreams when you can use .NET streams?

Because I have no idea what the difference is. Confused
Reply
#10
What Spaz means is that you can use all the .NET classes from MC++ just like you used to do in C#. So instead of ofstream which is the C++ unmanaged stream, you can use FileStream. Basically you can copy any C# code and make it MC++ with a few syntactical changes.

Unless you are doing it because that's what university\school\college told you that you should do, I would generally NOT recommend making managed applications in C++. Making C++ managed is ultimately destroying the whole point of the language, and you are rather off coding C# because it is much simpler. I know you are using managed C++ because the unmanaged version has no text boxes.
Reply
#11
Blast the difficulties of C++. I'm loving ColdFusion atm.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)