Click to See Complete Forum and Search --> : StreamReader delimiter in C++/CLI
File.Not.Found
March 5th, 2009, 10:17 AM
Hello to everybody, I am new to CodeGuru. I was wondering if anybody could give me a hand.
How can I specify delimiters for StreamReader so that it reads each single line from a csv file and then assigns each item between commas to a string of an array? I would like to achieve the same effect of the code below that I previously wrote in C++ and that was working fine (each line is read up to the 3rd item).
Any advice is welcome!
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++], '\,');
darwen
March 5th, 2009, 05:34 PM
StreamReader in itself won't do it, but combined with the Split method on System::String you should be able to do it easily
e.g.
// no '^' or gcnew : when this goes out of scope it'll be disposed.
StreamReader reader("c:\\mytestfile.txt", FileMode::Open);
String ^line = reader.ReadLine();
while (line != nullptr)
{
array<String ^> ^parts = line->Split(',');
// deal with array here
line = reader.ReadLine();
}
Darwen.
File.Not.Found
March 7th, 2009, 09:14 AM
Hi Darwen,
Thank you so much for your advice. I've tried as you suggest but I get this error:
Error 2 error C2664: 'System::IO::StreamReader::StreamReader(System::IO::Stream ^,bool)' : cannot convert parameter 1 from 'const char [11]' to 'System::IO::Stream ^'
This is my revised code:
StreamReader reader("INPUT3.csv", FileMode::Open);
String ^line = reader.ReadLine();
while (line != nullptr)
{
array<String^> ^cell = line->Split(',');
// deal with array here
int i = 0;
line = reader.ReadLine();
String^ StrValue0 = cell[i];
String^ StrValue1 = cell[i++];
String^ StrValue2 = cell[i++];
double Value0 = double:: Parse( StrValue0 );
textBoxPROVA->Text= Value0.ToString("N0");
double Value1 = double:: Parse( StrValue1 );
textBoxpostkf->Text= Value1.ToString("N0");
double Value2 = double:: Parse( StrValue2 );
textBoxpreks->Text= Value2.ToString("N0");
}
Thank you!
darwen
March 7th, 2009, 01:45 PM
Try this instead -
StreamReader reader("INPUT3.csv");
String ^line = reader.ReadLine();
while (line != nullptr)
{
Darwen.
File.Not.Found
March 7th, 2009, 03:42 PM
That's wonderful, thank you so much, now it works! The only problem is that
String ^line = reader.ReadLine();
reads all the lines present but keeps in memory only the last one. What I would need is a command to stop ReadLine() at the end of each line, so that I can return each substring.
File.Not.Found
March 12th, 2009, 05:47 PM
Hi Darwen,
I really appreciated your previous reply. I was wondering if you had any advice on how to stop reader.ReadLine() at the end of each line, rather than at the end of the array I created (please see my last post). Many thanks in advance.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.