StreamReader delimiters in C++/CLI
-
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 codeAny advice is welcome! ;)
-
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 codeAny advice is welcome! ;)
-
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 codeAny advice is welcome! ;)
-
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
-
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!
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
-
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
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.
-
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.
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