Reading .csv file

Questions regarding syntax

Reading .csv file

Postby Fisher » Thu Sep 16, 2010 12:20 am

I'm trying to read a .csv file exported from FileMaker Pro and then write a .txt file. The database contains my high school 50th reunion data and I need to extract that and put it into one paragraph per classmate in the .txt file. I've done this at my other reunions using Line Input # in VB6 and predecessors. Now I'm on an iMac and using KBasic, but there is no Line Input. The code snippet below works for one record but not for more than one.
I don't know OOP from oops, but maybe someone can help with KBasic code to accomplish the same thing. Thanks,

Steve Fisher
srfisher101@gmail.com

OPEN FName FOR INPUT AS #1
DO WHILE NOT EOF(#1)
INPUT #1, _
iCom, iPaid, iDate_Paid, _
iKey, iName, iAddress, iChild, iCity, iEmail, iEpub, iPhone, iPub,
iSpouse, iState, iText, iType, iUD, iYear, iZip
NameAddress = iName & ", " & iAddress & ", " & iCity & ", " & iState & " " & iZip & "."
LOOP
CLOSE #1
TextBox1.Text = "Finished

This code returns:
runtime error: 1 (file) could not be read in line 0
Unknow exception in KBasic runtime (kbrun).
Fisher
 
Posts: 2
Joined: Tue Sep 14, 2010 8:23 am

Re: Reading .csv file

Postby berndnoetscher » Thu Sep 16, 2010 8:13 am

Hello

There is a line input. The following code is an example shipped with Kbasic. Maybe this helps you.

Code: Select all

OPTION OLDBASIC


DIM REC$

CLS
OPEN KBasicDir() + "\examples\test\LISTE.TXT" FOR OUTPUT AS #1
DO
    INPUT "   NAME:       ", Name$  'input from keyboard
    INPUT "   Age:        ", Age$
    WRITE #1, Name$, Age$
    INPUT "Type a new entry"; R$
LOOP WHILE UCASE$(R$) = "Y"
CLOSE #1

'print content of file
OPEN KBasicDir() + "\examples\test\LISTE.TXT" FOR INPUT AS #1
CLS
PRINT "entries of file:": PRINT
DO WHILE NOT EOF(1)
    LINE INPUT #1, REC$ 
    PRINT REC$           
LOOP
CLOSE #1
berndnoetscher
Site Admin
 
Posts: 1059
Joined: Tue Sep 25, 2007 9:37 am

Re: Reading .csv file

Postby Fisher » Fri Sep 17, 2010 1:38 am

Thanks, I've made progress, but LINE INPUT seems to be trying to read the entire .csv file. I expected it to delineate each record at a CR-LF. Instead, it wants to read the entire file. My test exported file, from FileMaker Pro, has only two fields, City and State, and four records:
"Bethel","CT"
"Springfield","PA"
"Malvern","PA"
"New York","NY"
Yet the code below only reads one record and displays it as:
"Bethel","CT"
"Springfield","PA"
"Malvern","PA"
"New York","NY"
Note that there is a CR-LF between each record, but the problem is that if I export the entire 119kb database, it tries to read in the entire exported file, but only reads the first 65533 characters. The way I look at it, it should read in each record, as delineated by a CR-LF. Or, if it read in the entire exported file, which is 119kb, then I could program around that.
Here is the code:
OPTION OLDBASIC
Dim InFName, PathName, FName, BigField as string
Private Sub CommandButton2_OnEvent()
InFName = "TEST KRUG.csv"
PathName = "/Users/Steve/Documents/UDHS REUNIONS/FILEMAKER FILES/"
FName = PathName & "/" & InFName
OPEN FName FOR INPUT AS #1
LINE INPUT #1, BigField
TextBox1.Text = BigField
CLOSE #1
End Sub
Any help is appreciated... Can anyone give me an OOP way to do this?

Steve Fisher
srfisher101@gmail.com
Fisher
 
Posts: 2
Joined: Tue Sep 14, 2010 8:23 am

Re: Reading .csv file

Postby dave_e » Fri Sep 17, 2010 6:09 am

Hi Fisher

I think the loop around the Line Input is missing

' ... I've added some changes to the method (hopefully in bold, with the BigField to TextBox1 commented for now) and hope that it reads your data and writes each line to the screen on the basis that it works for me but I done said and heard that a zillion times already so if it doesn't no harm was intended.

Private Sub CommandButton2_OnEvent()
InFName = "TEST KRUG.csv"
PathName = "/Users/Steve/Documents/UDHS REUNIONS/FILEMAKER FILES/"
FName = PathName & "/" & InFName
CLS
OPEN FName FOR INPUT AS #1
Do While Not EOF(1) ' ... loop through lines.
LINE INPUT #1, BigField
Print BigField
Loop ' ... go for next line
'TextBox1.Text = BigField ' ... just commented for now.
CLOSE #1
End Sub

Hope this was helpful

Dave
dave_e
 
Posts: 10
Joined: Tue May 11, 2010 7:34 pm

Re: Reading .csv file

Postby pappawinni » Fri Sep 17, 2010 3:40 pm

Sorry Dave,

but I think that the problem could be that the file does not really contain CRLF because I had
been looking for a CSV file, found one and it was NOT separated by CRLF but only CR.
To test this you could try the following
(just create a new file in KBasic, insert the code, save it and then run it)

Attention.. changed Path because it ended with / and
when adding the file name you add / so.. suppose this was not correct.

Code: Select all
Sub main()
  Dim InFName as string
  Dim PathName as string
  Dim FName as string
  Dim BigField as string
  Dim j as integer
  Dim I as integer
  Dim iLenFile as integer
  Dim bytA as byte
 
  InFName = "TEST KRUG.csv"
  PathName = "/Users/Steve/Documents/UDHS REUNIONS/FILEMAKER FILES"
  FName = PathName & "/" & InFName
  i=1
  CLS
  OPEN FName FOR Binary AS #1
    iLenFile = lof(1)
    ? FName, iLenFile
    Do While i<= iLenFile ' ... loop through bytes.
      get #1, I, bytA
      select case bytA
        case 10:
           ? "Character No.: " & i & " Line Feed"
        case 13:
           ? "Character No.: " & i & " Carriage Return"
        case else:
      end select   
      i = i + 1
    Loop ' ... go for next byte
  CLOSE #1
End Sub

main()


alternatively:

Code: Select all
Sub TextFileInspect()
  Dim strInFile as string
  Dim strFilePath as string
  Dim strFileName as string
  Dim sFilter as string
  Dim strKey as string
  Dim strAsc as string
  Dim iByteCnt as integer
  Dim iLineCnt as integer
  Dim i as integer
  Dim iLenFile as integer
  Dim iMax as integer
  Dim bytA as byte
  Dim iChannel as integer
  Dim bolGo as boolean = true
  Dim iMsg as integer
 
  sFilter = "Text (*.doc *.txt)" & kbNewLine & "Any (*.*)"
 
  do
    iMsg = msgbox "Open Dialog will now ask for a file to view" , kbOKonly , "TEXT FILE INSPECT"
    strInFile = OpenDialog.GetFile("Select a file:", dir.homePath , sFilter )
    if strInFile = "" then
      bolGo = false
      iMsg = msgbox "Do you want to quit program ?" , kbYesNo , "No file selected"
      if iMsg = kbYes then exit sub
    else
      iMsg = msgbox strInfile , kbOKCancel , "File selected"
      bolGo = iif( iMsg = kbOK , true , false )
    end if
  loop while bolGo = false
 
  strFileName = file.name(strInFile)
  strFilePath = file.path(strInFile)

  i = 0
  iByteCnt = 1
  iLineCnt = 0
  strAsc = ""
  iChannel = freefile

  CLS
  OPEN strInFile FOR Binary AS iChannel
    iLenFile = lof(iChannel)
    iMax = ( iLenFile + 15 ) \ 16 * 16
    Do While i < iMax ' ... loop through bytes.
      if i < iLenFile then
        get iChannel, i, bytA
        select case bytA
          case 10:
            color(14)
          case 13:
            color(13)
          case else:
            color(15)
        end select
        strAsc = iif (bytA < 32 , strAsc & ".", strAsc & chr(bytA))
        ? Right("00" & hex(BytA)&" ",3); 
      else
        ? "   ";
      end if
      color(15)
      if iByteCnt = 8 then
        ? "- ";
      end if 
      if iByteCnt = 16 then
        ? strAsc : strAsc = "" : iByteCnt = 0
        iLineCnt = iLineCnt + 1
      end if
     
      i = i + 1
      iByteCnt = iByteCnt + 1
     
      if (iLineCnt = 25) and (i < iMax) then
        ? kbNewLine & "Press Key to continue, ESC to abort.."
        do
          strKey = inkey
        loop while strKey = ""
        if strKey = chr(27) then exit loop
        iLineCnt = 0
      end if 
    Loop ' ... go for next byte
  CLOSE iChannel
  ? kbNewLine
  ? strInFile
  ? strFilePath
  ? strFileName
End Sub

TextFileInspect()


And here how to read a file completely into a string and then split (reading into stringS)

Code: Select all
Sub Main()
  Dim InFName as string
  Dim PathName as string
  Dim FName as string
  Dim strInp as string
  dim stsLines as strings
  dim i as integer

  InFName = "TEST KRUG.csv"
  PathName = "/Users/Steve/Documents/UDHS REUNIONS/FILEMAKER FILES"

  FName = PathName & "/" & InFName
 
  'see if you have set the right codec
  'http://www.kbasic.com/doku.php?id=file#readtext
  '
  strInp = file.ReadText(fName,"ISO 8859-1",false,false)
  stsLines = split(strinp,chr(13),true,false)
 
  for i = 1 to stsLines.length
    ? stsLines.string(i)
  next
 
end sub

main()

2
Last edited by pappawinni on Sun Sep 19, 2010 9:44 pm, edited 4 times in total.
Pappa makes everything what otherwise none likes :)
pappawinni
 
Posts: 192
Joined: Tue Jan 19, 2010 11:27 pm
Location: Germany

Re: Reading .csv file

Postby dave_e » Fri Sep 17, 2010 6:56 pm

Yes Pappa, you must be right.

Thanks
Dave
dave_e
 
Posts: 10
Joined: Tue May 11, 2010 7:34 pm


Return to Coding Questions

cron