Hi,
first you should know that this is a real NOT trivial problem!
But it is solvable in nearly all cases. %-)
you need to consider about these facts...
1. what kind of CSV-file do you have. Are text and numeric values distinguished by a " or ' ?
2. If text values are enclosed by " or ' is a value of the enclosing char marked special in a text(string)? like /" or ~"
3. How are numeric values written in the CVS? eg a value of 1.53 can be written as 1.53 or 1,53 which is depending on local settings in your machine... which does not have to be mandatory the same for all users!!!
I think there can be more than tese 3 pitfals using a CVS-File.
Now let us solve the proplem from the worts case to the easiest.
If all or some of the above ( or any other) problem can occur you need to code all the cases and handle it by your own. This means you need to code a parser like this (pseudocode)
Code:
FileRead( #file , ls_input)
l_count = 1
l_len = len( the_string_fom file)
FOR l_pos = 1 TO l_len
my_char = mid( ls_in, l_pos, 1)
if reading_variable = no then
reading_variable = numeric
if my_char = " then reading_variable = string
if my_char = , then reading_variable = no; var_no ++
end if
if reading_variable = numeric then
line_var[var_no]+= my_char
// and here you need to handle a possible colon as decimal separator
end if
if reading_variable = string then
line_var[var_no]+= my_char
// and here you need to handle a possible " as part of the stringn or set reading_variable = no if end of variable is reached ( closing " )
end if
next
you see this can be very hard to be done and end in a long coding all cases of colons and points and decimals, quotes etc.
my be you'e in luck and the cvs is of good quality. this means:
all strings are quoted and no commy appear within a string.
decimals are written as 1.53 and NOT 1,53
you can use a function like this one to parese your imput_string
Code:
function string of_get_token (ref string as_string, string as_token);string ls_ret = "", ls_string, ls_links
long ll_token_laenge, ll_token_pos
ls_string = as_string + as_token
ll_token_laenge = LEN( as_token )
ll_token_pos = POS( ls_string , as_token )
IF ll_token_laenge > 0 THEN
IF ll_token_pos > 0 THEN
ls_ret = LEFT( ls_string , ll_token_pos -1 )
ls_links = MID( as_string , ll_token_pos + ll_token_laenge)
as_string = ls_links
END IF
END IF
RETURN ls_ret
If you can manage it that the source file you want to read can be produced as a TAB-delimeted file instead of comma or semicolon-separated as CSV Than you can easily import the file into a datawindowand than parse your datawindow for getting the values.
hope you are not disapointed that there is no way for generic function for all possible filesformats and pitfals within CSV.
It Is really recomended - as far as I can see it - NOT to use CVS. It is much better to use TAB-deleimited files or Files with known fixed length parted rows as you had.
if you need to stay with CSV you should google with 'reading comma separated' or 'importing svc' and check if some forums or source-sites hava a code sample. this can be vc++ or basic or pascal or any. maybe there is a codesample handling the cases discussed on top
good luck