dealing with a CSV file

dealing with a CSV file

Postby hag » Sat May 15, 2010 5:22 pm

I am importing a comma delimited file and I need to make certain the user has set up the file properly.
Is there a way to count the delimiters (commas) in the file and fix it if it has too may delimiters. All has to be at run time and behind the scenes.
Thank you
Harvey
hag
 
Posts: 598
Joined: Tue Apr 15, 2008 4:51 pm
Location: LOs Angeles, California

Re: dealing with a CSV file

Postby Antonio Linares » Sat May 15, 2010 7:11 pm

Harvey,

You can use NumAt() to count the commas in each line:

NUMAT(<cStringToMatch>, <cString>, [<nIgnore>]) --> nCount

To extract each line, in a fast way, you can use this code:
Code: Select all  Expand view

       local cText := MemoRead( "text.csv" ), cLine
       local nLen := Len( cText )
       local nFrom := 1

       While nFrom <= nLen
          cLine  := ExtractLine( cText, @nFrom )
          if NumAt( ",", cLine ) != 5 // number of ","
             MsgAlert( "error in line " + Str( nFrom - 1 ) )
          endif
       end
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: dealing with a CSV file

Postby hag » Sat May 15, 2010 9:57 pm

Thanks Antoonio.

I'll give it a try tomorrow.
Thank you
Harvey
hag
 
Posts: 598
Joined: Tue Apr 15, 2008 4:51 pm
Location: LOs Angeles, California

Re: dealing with a CSV file

Postby hag » Sat May 15, 2010 10:47 pm

Antonio

Where is the function numat() located. Won't compile.
Thank you
Harvey
hag
 
Posts: 598
Joined: Tue Apr 15, 2008 4:51 pm
Location: LOs Angeles, California

Re: dealing with a CSV file

Postby Antonio Linares » Sat May 15, 2010 11:02 pm

Harvey,

If you are using Harbour then you have to link hbct.lib, if you use xHarbour then link ct.lib
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Marc Venken, nageswaragunupudi and 63 guests