-
Comma delimited File
I am trying to use StreamReader to read a file and to separate the lines that I read by commas (Del).
I think I have managed to Read the file so I will have the Line in the StringLine.
Now is my problem how I will put the values to the Vectors with the ->Add method.
How will I separate out this line by Commas to do this ?
text1,1.1,2.2
text2,3.3,4.4
Code:
StreamReader^ File1 = gcnew StreamReader( "D:\\Files\\OneFile.txt" );
String^ Del = ",";
String^ StringLine;
String^ Text;
List<String^>^ String1 = gcnew List<String^>();
List<double> Value1 = gcnew List<double>();
List<double> Value2 = gcnew List<double>();
while ( File1->Peek() >= 0 )
{
StringLine = File1->ReadLine();
Day1->Add( );
Value1->Add( );
Value2->Add( );
}
-
You can split the string via comma. I don't know the CLI syntax but I do know the C# so I will do that instead.
Code:
string[] commaArray = StringLine.split(',');
for(int i = 0; i < commaArray.length; i++){
Day1.Add(commaArray[i]);
}//for
-
So you need to find each comma in order, and copy the value starting from the previous comma, to the newly found comma into the new string.
std::string has a constructor that a string and a range of values to copy over. string::find() will find the first occurrence of character starting from a given index. This can be used to find the position of each comma by setting the starting index to the position of the previous comma.
-
prog-bman and King Mir...
This should be the solution. It seems to work great. Thanks a lot for your help !
-
Anytime. You are welcome.
-
Actually, this sounds more like a job for stringstreams, if you were to ask me. But it sounds like you already have the code working, so perhaps someone else with similar may gain more help from my option.