Discussion:
comma delimited format
(too old to reply)
JV
2007-06-17 14:44:25 UTC
Permalink
I want to read a comma delimited text file (csv)

How can I have access directly to one of the fields between the ';' ?

Thank you!

Jan
Thomas Maeder [TeamB]
2007-06-17 20:12:04 UTC
Permalink
Post by JV
I want to read a comma delimited text file (csv)
How can I have access directly to one of the fields between the ';' ?
What now: comma or semicolon?

What do you mean by "directly"?
chenzero
2007-06-18 01:51:36 UTC
Permalink
Hello JV,

Here is some pseudo code, and I am sure that
you will find more interesting thing in the way to
finish it.

#include <string>
#include <vector>
#include <iostream>
using namespace std;

vector<string> fields;

string line = "f1;f2;f3";
StringTokenizer st(line, ";");
// a string tokenizer which separates the line by ";"

while(st.hasNext()) { // test if has next field
string f = st.next(); // get the next field
fields.add(f); // add in list
}

// now, I think you can "directly" access the field.
// but must ensure the index is not out of scope.

cout<< fields[0] ; // fields[0] should be equal "f1"
Post by JV
I want to read a comma delimited text file (csv)
How can I have access directly to one of the fields between the ';' ?
Thank you!
Jan
JV
2007-06-18 20:30:25 UTC
Permalink
Hi Thomas and Chenzero;

I meant semicolon,
with 'directly' I meant read only a specific part (between some ';' of the
string) (sorry)

I checked for info about
StringTokenizer st(line, ";");
but I could not find anything

So I tried to write my own GetField function:

AnsiString __fastcall GetField(const AnsiString str, int index)
{
String Field = "";
int len = str.Length();
int i = 1;
int t = 0; // count number of ';' in str
for (i=1; i<len; i++)
{
if (str[i] == ';') t++;
if (t == index)
{
if (t != 0)i++;
while (str[i] != ';' && i<len)
{
Field += str[i];
i++;
if (i==len) Field += str[i];
}
i = len; // jump out
}
}
return Field;
}

it seems to work, also with index = 0 or index higher dan number of
semicolons in str

Thank you for helping me

Bye,

Jan
chenzero
2007-06-19 15:00:07 UTC
Permalink
Hello JV,
Post by JV
Hi Thomas and Chenzero;
I checked for info about
StringTokenizer st(line, ";");
but I could not find anything
there is a wonderful search engine http://www.google.com
believe me, it can always search something out for anything. :)
eg:
http://www.codeproject.com/cpp/string_tokenizer_class_in_c__.asp

Regards,
chenzero
JV
2007-06-19 18:29:28 UTC
Permalink
Post by chenzero
there is a wonderful search engine http://www.google.com
believe me, it can always search something out for anything. :)
http://www.codeproject.com/cpp/string_tokenizer_class_in_c__.asp
Hm? Google? never heard of it...
(joke)

I searched on Tokeniser (reminds me on "Womaniser")
but I found only Java stuff

Thanks Chenzero

Jan
Duane Hebert
2007-06-19 18:52:49 UTC
Permalink
Post by JV
Post by chenzero
there is a wonderful search engine http://www.google.com
believe me, it can always search something out for anything. :)
http://www.codeproject.com/cpp/string_tokenizer_class_in_c__.asp
Hm? Google? never heard of it...
(joke)
I searched on Tokeniser (reminds me on "Womaniser")
but I found only Java stuff
You may find this one interesting as well...
http://boost.org/libs/tokenizer/index.html

Continue reading on narkive:
Loading...