Southperry.net
C++ Strings - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: C++ Strings (/showthread.php?tid=29397)



C++ Strings - Hazzy - 2010-08-23

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?


C++ Strings - Fiel - 2010-08-23

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.


C++ Strings - Russt - 2010-08-23

"<<" should be able to output a string to a file. What type is Output->Text?


C++ Strings - Hazzy - 2010-08-23

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)



C++ Strings - Fiel - 2010-08-23

Post everything from the method, please.


C++ Strings - Hazzy - 2010-08-23

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.


C++ Strings - Russt - 2010-08-23

Edit: Oh this is Visual C++.

I wouldn't know, but try something in here
http://msdn.microsoft.com/en-us/library/system.string_methods.aspx


C++ Strings - Spaz - 2010-08-24

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?


C++ Strings - Hazzy - 2010-08-24

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

Because I have no idea what the difference is. Confused


C++ Strings - Kortestanov - 2010-08-25

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.


C++ Strings - IImaplers - 2010-08-26

Blast the difficulties of C++. I'm loving ColdFusion atm.