Code archives/File Utilities/Read Comma Delimited String

This code has been declared by its author to be Public Domain code.

Download source code

Read Comma Delimited String by Ziltch2003
ReadCSVString$(Stream) works like ReadString(stream) but looks for data enclosed in quotes not in blitz basic's string format.

Example:


  type quotes
    field quote$
    field Author$
    field era$
    field Num
  end type

  quotefile=readfile("smallquotes.txt")
  while not eof(quotefile)
    tq.quotes  = new quotes
    tq\quote$  = ReadCSVString$(quotefile)
    tq\Author$ = ReadCSVString$(quotefile)
    tq\era$    = ReadCSVString$(quotefile)
    count = count + 1
    tq\Num = count
;    debuglog count + " "+tq\quote$+" "+tq\Author$+" "+tq\era$
  wend
  closefile(quotefile)


smallquotes.txt
"Beware the Jabberwock, my son!","Lewis Carroll","1832-98"
"Though it rain daggers with their points downward.","Burton, Robert","1576-1640"
"A String","Another String","etc"

The code looks for data inside quotes so the data can be delimited by any character, not just commas. TAB delimited should work as well.

Make sure the date does not contain double quotes " in the strings as this will confuse it!
Function ReadCSVString$(Stream)
; ADAmor Ziltch Nov 2003

  local qte$,Ccnt,byt,char$
  qte$=chr(34)
  OutString$ = ""
  Ccnt = 0
  while char$<>qte$ and eof(stream)=0
    Ccnt = Ccnt + 1
    byt= readbyte(stream)
    char$=chr(byt)
  wend
  char$=""
  while char$<>qte$ and eof(stream)=0
    Ccnt = Ccnt + 1
    byt= readbyte(stream)
    char$=chr(byt)
    if char$ <> qte$ then OutString$=OutString$+char$
  wend
  
  return OutString$
End Function

Comments

None.

Code Archives Forum