Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Spliting a String in C++/CLI

Spliting a String in C++/CLI

Scheduled Pinned Locked Moved Managed C++/CLI
c++tutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T RATHA KRISHNAN
    wrote on last edited by
    #1

    Hi! I've a file called test.ini. The contents of this file is: PreviousCheck =true LID =ratha PWD =rtrrules I've to read the file and assign each value after the equal(=) sign to three System::String^ variables. I don't want the text preceding the equal sign(i.e PreviousCheck =,LID = and PWD = all I don't want). How to do this in C++/CLI?

    G J 2 Replies Last reply
    0
    • T T RATHA KRISHNAN

      Hi! I've a file called test.ini. The contents of this file is: PreviousCheck =true LID =ratha PWD =rtrrules I've to read the file and assign each value after the equal(=) sign to three System::String^ variables. I don't want the text preceding the equal sign(i.e PreviousCheck =,LID = and PWD = all I don't want). How to do this in C++/CLI?

      G Offline
      G Offline
      Ger Hayden
      wrote on last edited by
      #2

      Firstly, what you are doing is better suited to the app.config key/value approach. But to answer the question investigate the 'Split' method on the String^ class in C++/CLI to achieve what you want.

      Ger

      T 1 Reply Last reply
      0
      • G Ger Hayden

        Firstly, what you are doing is better suited to the app.config key/value approach. But to answer the question investigate the 'Split' method on the String^ class in C++/CLI to achieve what you want.

        Ger

        T Offline
        T Offline
        T RATHA KRISHNAN
        wrote on last edited by
        #3

        Hi! I used the following code to get a line from the file(and then I will split it into parts).

        System::IO::StreamReader reader("test.ini", System::IO::FileMode::Open);
        System::String^ line = reader.ReadLine();
        

        But I got the following error: error C2664: 'System::IO::StreamReader::StreamReader(System::IO::Stream ^,bool)' : cannot convert parameter 1 from 'const char [17]' to 'System::IO::Stream ^' Reason: cannot convert from 'const char *' to 'System::IO::Stream ^' No user-defined-conversion operator available, or Cannot convert an unmanaged type to a managed type How to get rid of this error?

        G L 2 Replies Last reply
        0
        • T T RATHA KRISHNAN

          Hi! I used the following code to get a line from the file(and then I will split it into parts).

          System::IO::StreamReader reader("test.ini", System::IO::FileMode::Open);
          System::String^ line = reader.ReadLine();
          

          But I got the following error: error C2664: 'System::IO::StreamReader::StreamReader(System::IO::Stream ^,bool)' : cannot convert parameter 1 from 'const char [17]' to 'System::IO::Stream ^' Reason: cannot convert from 'const char *' to 'System::IO::Stream ^' No user-defined-conversion operator available, or Cannot convert an unmanaged type to a managed type How to get rid of this error?

          G Offline
          G Offline
          Ger Hayden
          wrote on last edited by
          #4

          I suggest putting "Test.INI" in a variable defined as String^ e.g.

          String^ FileName;
          FileName = String::Format("Test.INI");

          Finally when you are splitting your input strings define the index character as wchar_t

          Ger

          1 Reply Last reply
          0
          • T T RATHA KRISHNAN

            Hi! I've a file called test.ini. The contents of this file is: PreviousCheck =true LID =ratha PWD =rtrrules I've to read the file and assign each value after the equal(=) sign to three System::String^ variables. I don't want the text preceding the equal sign(i.e PreviousCheck =,LID = and PWD = all I don't want). How to do this in C++/CLI?

            J Offline
            J Offline
            John Schroedl
            wrote on last edited by
            #5

            This seems to work:

            #include "stdafx.h"

            using namespace System;

            int main(array<String ^> ^args)
            {
            array<String^>^ lines = System::IO::File::ReadAllLines("C:\\temp\\test.ini", System::Text::Encoding::UTF8);

            Console::WriteLine("Key\\t\\tValue");
            
            for each (String^ line in lines)
            {
            	if (String::IsNullOrWhiteSpace(line))
            		continue;
            
            	int offset = line->LastIndexOf("=");
            	if (offset >= 0) 
            	{
            		String^ key = line->Substring(0, offset)->Trim();
            		String^ value = line->Substring(offset+1)->Trim();
            
            		Console::WriteLine(key + "\\t\\t" + value);
            	}
            }
            
            return 0;
            

            }

            John

            1 Reply Last reply
            0
            • T T RATHA KRISHNAN

              Hi! I used the following code to get a line from the file(and then I will split it into parts).

              System::IO::StreamReader reader("test.ini", System::IO::FileMode::Open);
              System::String^ line = reader.ReadLine();
              

              But I got the following error: error C2664: 'System::IO::StreamReader::StreamReader(System::IO::Stream ^,bool)' : cannot convert parameter 1 from 'const char [17]' to 'System::IO::Stream ^' Reason: cannot convert from 'const char *' to 'System::IO::Stream ^' No user-defined-conversion operator available, or Cannot convert an unmanaged type to a managed type How to get rid of this error?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              T.RATHA KRISHNAN wrote:

              How to get rid of this error?

              I think this is a subtle one; you have provided a char* in parameter 1 of the constructor, but no such constructor exists. You need to use a String object as the path parameter for the compiler to select the correct constructor. You can find all the gory details here[^] on MSDN.

              I must get a clever new signature for 2011.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups