CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2009
    Posts
    3

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ Sorting a string and inserting text.

    Quote Originally Posted by Gibson_Junk View Post
    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

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: C++ Sorting a string and inserting text.

    [ Moved thread ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.
    Last edited by Eri523; January 22nd, 2013 at 08:46 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured