I am using StreamReader to read a File. With the code below I am able to read a whole line.
What I wonder is how it is possible to read a comma delimited file like these lines.
How will I put the delimiter ',' and also assign the values to the String^ and doubles ?

Mon,1,2,3
Tues,4,5,6
Wed,7,8,9

String^ line;
String^ Day;
double Number1, Number2, Number3;

 //Create an instance of StreamReader to read from a file.
StreamReader^ sr = gcnew StreamReader( "C:\\File.txt" );

         
// Read and display lines from the file until the end of  the file is reached.
while ( line = sr->ReadLine() )
 {
            


 }

Found this using google: Example

1  
   2    private void ImportCountries()
   3    {
   4      string delimiter = ",";
   5      string fileName = @"c:\countrylist3.csv";
   6  
   7      StreamReader sr = new StreamReader(fileName);
   8  
   9      try
  10      {
  11        while (sr.Peek() >= 0)
  12        {
  13          string r = sr.ReadLine();
  14          string[] items = r.Split(delimiter.ToCharArray());
  15        }
  16      }
  17      finally
  18      {
  19        sr.Close();
  20      }
  21    }
  22

As I red, Peek() >= 0 will check if the eof file is reached like this.
However I dont really understand how it works when the line, "Line" will be delimited by the "Delimiter".
What comes out of the line:
string[] items = r.Split(delimiter.ToCharArray());

I am not sure what happens here. I would like to split the line "r" by Commas and put the First to Day, second, third and fourth value to the double Numbers.

//Create an instance of StreamReader to read from a file.
StreamReader^ sr = gcnew StreamReader( "C:\\File.txt" );

String^ r;
String^ Day;
String^ Delimiter = ",";
double Number1, Number2, Number3;

 while ( sr->Peek() >= 0 )
{
	 r = sr->ReadLine();

            
}

I belive it stores array of strings (all read froom same line, but separated with Delimiter), but am not sure. Try it

Ok, I thought something like that too. I will use vectors to store the values I read with the delimiter. I am guessing out some code but are not sure if I am on the right track to do as I do.
What I have done is to declare some Managed vectors and also the elements for these(40).
Then I am using a countdown(x) to put values into the elements.
The compiler says that it cant convert parameneter 1 from String^to wchar_t regarding this line:
I am not very used to the managed classes. The ->Split seems to be used within arrays.
Wonder if there is a method to put it to these vectors instead.

Line->Split(Delimiter->ToString());

StreamReader^ sr = gcnew StreamReader( "C:\\File.txt" );

String^ Delimiter = ",";
String^ Line;
String^ Day;

List<String^>^ Day= gcnew List<String^>(40);
List<double>^ Num1 = gcnew List<double>(40);
List<double>^ Num2 = gcnew List<double>(40);
List<double>^ Num3 = gcnew List<double>(40);

int x = 30;

// Read and display lines from the file 
		 
while ( sr->Peek() >= 0 && x > 2 )
{
	 x = x - 1;
	 Line = sr->ReadLine();
	 Day[x] = Line->Split(Delimiter->ToString());
	 Num1[x] = Line->Split(Delimiter->ToString());
	 Num2[x] = Line->Split(Delimiter->ToString());
	 Num3[x] = Line->Split(Delimiter->ToString());
}

I belive it stores array of strings (all read froom same line, but separated with Delimiter), but am not sure. Try it

When reading a file I am trying to split the line and put the comma delimited values into vectors.
It should be something I am doing wrong with the splitting of the line. It seems that ->Split only is used for Arrays but are not sure ? The thing is that I want these values to be ->Add in the vectors.

Mon,1,2,3
Tues,4,5,6
Wed,7,8,9

//Create an instance of StreamReader to read from a file.
StreamReader^ sr = gcnew StreamReader( "C:\\File.txt" );

String^ Delimiter = ",";
String^ Line;
String^ Day;

List<String^>^ Day1 = gcnew List<String^>();
List<double>^ Num1 = gcnew List<double>();
List<double>^ Num2 = gcnew List<double>();
List<double>^ Num3 = gcnew List<double>();



// Read lines from the file 		 
while ( sr->Peek() >= 0 )
{

 Line = sr->ReadLine();
 Day1->Add( Line->Split(Delimiter->ToString()) );
 Num1->Add( System::Convert::ToDouble(Line->Split(Delimiter->ToString()) );
 Num2->Add( System::Convert::ToDouble(Line->Split(Delimiter->ToString()) );
 Num3->Add( System::Convert::ToDouble(Line->Split(Delimiter->ToString()) );
}

Found a solutions to 'Pos' Positions for the ',' to parse out the values like this. Suppose this could be a solution.

//Create an instance of StreamReader to read from a file.
StreamReader^ sr = gcnew StreamReader( "C:\\File.txt" );

String^ Delimiter = ",";
String^ Line;
String^ Day;
int Pos = 0;

List<String^>^ Day1 = gcnew List<String^>();
List<double>^ Num1 = gcnew List<double>();
List<double>^ Num2 = gcnew List<double>();
List<double>^ Num3 = gcnew List<double>();


while ( sr->Peek() >= 0 )
{

 Line = sr->ReadLine();
 Pos = 0;



Day1->Add( Line->Substring(Pos, Line->IndexOf(",")) ); Pos = Line->IndexOf(",", Pos)+1;
Num1->Add( System::Convert::ToDouble(Line->Substring(Pos, Line->IndexOf(",", Pos) - Pos)) ); Pos = Line->IndexOf(",", Pos)+1; 
Num2->Add( System::Convert::ToDouble(Line->Substring(Pos, Line->IndexOf(",", Pos) - Pos)) ); Pos = Line->IndexOf(",", Pos)+1;
Num3->Add( System::Convert::ToDouble(Line->Substring(Pos, Line->IndexOf(",", Pos) - Pos)) ); Pos = Line->IndexOf(",", Pos)+1;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.