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
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. StreamReader delimiters in C++/CLI

StreamReader delimiters in C++/CLI

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structuresjsontutorialquestion
7 Posts 3 Posters 1 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.
  • J Offline
    J Offline
    J_E_D_I
    wrote on last edited by
    #1

    Hi, I am trying to make my application read fields from a string array (by reading from a .csv file), assign each single field to a new string and then cast it double. I am however having problems setting the delimiters in C+/CLI. The code I previously wrote for C++ (reported at the end) was working file, but I can't find anywhere how to set similar delimiters using C++/CLI.

    // Create string array
    array<String^>^ Cell= gcnew array<String^>(100);

    StreamReader^ sr = gcnew StreamReader( "INPUT.csv" );

    // Read up to the end of the file
    while(sr->Peek() >= 0)
    {
    for(int i = 0; i<100; i++)
    {
    // Missing code! What I don't know..
    // Should I use Cell= sr.ReadLine().Split(delimiter.ToCharArray()); ?

    String^ Value0AsString= Cell[i++];
    String^ Value1AsString= Cell[i++];
    String^ Value2AsString= Cell[i++];

    double Value0= double:: Parse( Value0AsString);
    double Value1= double:: Parse( Value1AsString);
    double Value2= double:: Parse( Value2AsString);
    }
    }

    The delimiters I would like to use are the following (code in c++):

    string Cell[100];
    int i = 0;
    ifstream INPUT ("INPUT.csv");
    while (! INPUT.eof())
    {
    if ((i + 1) % 3 == 0) getline(INPUT, Cell[i++]);
    else getline(INPUT, Cell[i++], '\,');
    }
    ....rest of the code

    Any advice is welcome! ;)

    K D 2 Replies Last reply
    0
    • J J_E_D_I

      Hi, I am trying to make my application read fields from a string array (by reading from a .csv file), assign each single field to a new string and then cast it double. I am however having problems setting the delimiters in C+/CLI. The code I previously wrote for C++ (reported at the end) was working file, but I can't find anywhere how to set similar delimiters using C++/CLI.

      // Create string array
      array<String^>^ Cell= gcnew array<String^>(100);

      StreamReader^ sr = gcnew StreamReader( "INPUT.csv" );

      // Read up to the end of the file
      while(sr->Peek() >= 0)
      {
      for(int i = 0; i<100; i++)
      {
      // Missing code! What I don't know..
      // Should I use Cell= sr.ReadLine().Split(delimiter.ToCharArray()); ?

      String^ Value0AsString= Cell[i++];
      String^ Value1AsString= Cell[i++];
      String^ Value2AsString= Cell[i++];

      double Value0= double:: Parse( Value0AsString);
      double Value1= double:: Parse( Value1AsString);
      double Value2= double:: Parse( Value2AsString);
      }
      }

      The delimiters I would like to use are the following (code in c++):

      string Cell[100];
      int i = 0;
      ifstream INPUT ("INPUT.csv");
      while (! INPUT.eof())
      {
      if ((i + 1) % 3 == 0) getline(INPUT, Cell[i++]);
      else getline(INPUT, Cell[i++], '\,');
      }
      ....rest of the code

      Any advice is welcome! ;)

      K Offline
      K Offline
      ky_rerun
      wrote on last edited by
      #2

      You could just your regularexpressions and not worry with parsing it yourself.


      a programmer traped in a thugs body

      1 Reply Last reply
      0
      • J J_E_D_I

        Hi, I am trying to make my application read fields from a string array (by reading from a .csv file), assign each single field to a new string and then cast it double. I am however having problems setting the delimiters in C+/CLI. The code I previously wrote for C++ (reported at the end) was working file, but I can't find anywhere how to set similar delimiters using C++/CLI.

        // Create string array
        array<String^>^ Cell= gcnew array<String^>(100);

        StreamReader^ sr = gcnew StreamReader( "INPUT.csv" );

        // Read up to the end of the file
        while(sr->Peek() >= 0)
        {
        for(int i = 0; i<100; i++)
        {
        // Missing code! What I don't know..
        // Should I use Cell= sr.ReadLine().Split(delimiter.ToCharArray()); ?

        String^ Value0AsString= Cell[i++];
        String^ Value1AsString= Cell[i++];
        String^ Value2AsString= Cell[i++];

        double Value0= double:: Parse( Value0AsString);
        double Value1= double:: Parse( Value1AsString);
        double Value2= double:: Parse( Value2AsString);
        }
        }

        The delimiters I would like to use are the following (code in c++):

        string Cell[100];
        int i = 0;
        ifstream INPUT ("INPUT.csv");
        while (! INPUT.eof())
        {
        if ((i + 1) % 3 == 0) getline(INPUT, Cell[i++]);
        else getline(INPUT, Cell[i++], '\,');
        }
        ....rest of the code

        Any advice is welcome! ;)

        D Offline
        D Offline
        dybs
        wrote on last edited by
        #3

        You are correct, the String.Split method is what you want. See the documentation[^] for an example of how to set up your delimiters in Managed C++. Dybs

        J 1 Reply Last reply
        0
        • D dybs

          You are correct, the String.Split method is what you want. See the documentation[^] for an example of how to set up your delimiters in Managed C++. Dybs

          J Offline
          J Offline
          J_E_D_I
          wrote on last edited by
          #4

          Hi Dybs, thanks for the reply. However I can't find any good example on the correct syntax for the String.Split method in C++/CLI...Any suggestion is welcome!

          D 1 Reply Last reply
          0
          • J J_E_D_I

            Hi Dybs, thanks for the reply. However I can't find any good example on the correct syntax for the String.Split method in C++/CLI...Any suggestion is welcome!

            D Offline
            D Offline
            dybs
            wrote on last edited by
            #5

            The link in my previous post is to the MSDN documentation for the String.Split method. Click on the first overload of the method (String(char[])), and scroll about 2/3 to the bottom. You will see examples of how to use the function in VB, C#, and Visual C++ (same thing as C++/CLI). The example should give you what you need. Dybs

            J 1 Reply Last reply
            0
            • D dybs

              The link in my previous post is to the MSDN documentation for the String.Split method. Click on the first overload of the method (String(char[])), and scroll about 2/3 to the bottom. You will see examples of how to use the function in VB, C#, and Visual C++ (same thing as C++/CLI). The example should give you what you need. Dybs

              J Offline
              J Offline
              J_E_D_I
              wrote on last edited by
              #6

              Hi Dybs, many thanks for the clarification. Now I manage to split the string but still I am unable to set delimiters for StreamReader. I've looked extensively on the web but I can't see pertinent examples for C++/CLI. I would need StreamReader (or

              String ^line = reader.ReadLine();

              ) to read up to the 3rd value of a CSV file and in another instance I need it to stop reading when it meets the end of the line. Do you have any idea on how to specify these delimiters please? I'd really appreciate your help.

              D 1 Reply Last reply
              0
              • J J_E_D_I

                Hi Dybs, many thanks for the clarification. Now I manage to split the string but still I am unable to set delimiters for StreamReader. I've looked extensively on the web but I can't see pertinent examples for C++/CLI. I would need StreamReader (or

                String ^line = reader.ReadLine();

                ) to read up to the 3rd value of a CSV file and in another instance I need it to stop reading when it meets the end of the line. Do you have any idea on how to specify these delimiters please? I'd really appreciate your help.

                D Offline
                D Offline
                dybs
                wrote on last edited by
                #7

                Please read the documentation more closely. In the link I posted previously, you'll notice several overloads of String.Split. If you bother to read the descriptions of the overloads, you'll see there's one that fits your requirements quite nicely. All I'll say is you need one overload to split the string to the end of the line, and another to split the string and only get the first 3 values (or however many you want). Again, you can find the sample code in the documentation. For future reference, the MSDN documentation is your friend. You could have figured this out on your own in 5 minutes by Googling "System.String" and looking at the functions available for the string class. If you need to know what functions are available for any .NET class, Google the fully-qualified name of the class (namespace.class) and the first couple of links will give you exactly what you need. Dybs

                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