C++ Sorting a string and inserting text.
I have a program like this that I made.
Code:
outfile.open ("ImportUsers.txt");
if (infile.good())
{
while (!infile.eof())
{
infile >> First >> Rank >> Last;// Saves the line in STRING.
cout << "dn: CN=" << First << " " << Rank << " " << Last << dcClass << endl;
As you can see it reads the first line of the file and breaks it at each white space into a separate string. I wanted to transform this into a win forms app but I have run into a issue.
Code:
public: System::Void convertToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
StringBuilder^ sb = gcnew StringBuilder();
Convertor^ form2 = gcnew Convertor();
if(listBox1->Items->Count > 0){
for (int i = 0; i < listBox1->Items->Count; i++){
String^ temp = listBox1->Items[i]->ToString();
sb->AppendFormat("{0}", temp)->AppendLine();
}
sb->Length = sb->Length - 1;
form2->textBox1->Text = sb->ToString();
form2->ShowDialog();
} else {
MessageBox::Show("You must open a file before converting it!", "Error");
}
}
How can I do that same thing here? I am lost.
Re: C++ Sorting a string and inserting text.
Quote:
Originally Posted by
Gibson_Junk
I have a program like this that I made.
That code is not C++. You want the Managed C++ forum, not this one.
Code:
System::Void convertToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
That code is gibberish here. You are using a language called Managed C++, and there is a forum dedicated to the usage of this language. This forum is for traditional ANSI C++ using the Visual C++ compiler and libraries such as MFC.
Regards,
Paul McKenzie
Re: C++ Sorting a string and inserting text.
Re: C++ Sorting a string and inserting text.
I don't really see the connection between your two code snippets, regarding what they're supposed to do.
This is a C++/CLI console app rather than a Windows Forms app, but functionally it's roughly equivalent to your first snippet (the native one):
Code:
// Test26.cpp: Hauptprojektdatei.
#include "stdafx.h"
using namespace System;
using namespace System::IO;
int main(array<System::String ^> ^args)
{
StreamReader ^sr = gcnew StreamReader("ImportUsers.txt");
while (!sr->EndOfStream) {
String ^strLine = sr->ReadLine();
array<String ^> ^astrWords = strLine->Split();
Console::WriteLine("dn: CN={0}", String::Join(" ", astrWords));
}
Console::WriteLine();
Console::WriteLine("Hit <Enter> to continue...");
Console::ReadLine();
return 0;
}
The main difference is that it splits the input line components into items in a string array rather than individual named variables. At my stage of knowledge of your scenario, that's rather an advantage than a drawback, since it works with any number of components on a line and the named variables in your snippet are just used to transport the components from input to output anyway. It also ignores your dcClass for the simple reason that I have no idea what it is.
Also, what's the point in splitting the input line by spaces and then putting the components together using spaces for output? In this respect, this is about equivalent to my sample above:
Code:
// Test26.cpp: Hauptprojektdatei.
#include "stdafx.h"
using namespace System;
using namespace System::IO;
int main(array<System::String ^> ^args)
{
StreamReader ^sr = gcnew StreamReader("ImportUsers.txt");
while (!sr->EndOfStream) {
String ^strLine = sr->ReadLine();
Console::WriteLine("dn: CN={0}", strLine);
}
Console::WriteLine();
Console::WriteLine("Hit <Enter> to continue...");
Console::ReadLine();
return 0;
}
So you see you need to tell us more about what you're actually intending to do for us to be able to help us, unless this post already gave you a sufficient starting point.