November 28, 2020, 02:17:32 AM

Author Topic: [bb] Read Comma Delimited String by Ziltch [ 1+ years ago ]  (Read 559 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Read Comma Delimited String
Author : Ziltch
Posted : 1+ years ago

Description : ReadCSVString$(Stream) works like ReadString(stream) but looks for data enclosed in quotes not in blitz basic's string format.

Example:

Code: [Select]

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

  quotefile=readfile("smallquotes.txt")
  while not eof(quotefile)
    tq.quotes  = new quotes
    tqquote$  = ReadCSVString$(quotefile)
    tqAuthor$ = ReadCSVString$(quotefile)
    tqera$    = ReadCSVString$(quotefile)
    count = count + 1
    tqNum = count
;    debuglog count + " "+tqquote$+" "+tqAuthor$+" "+tqera$
  wend
  closefile(quotefile)


smallquotes.txt
Code: [Select]
"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! [/i]

Code :
Code: BlitzBasic
  1. Function ReadCSVString$(Stream)
  2. ; ADAmor Ziltch Nov 2003
  3.  
  4.   local qte$,Ccnt,byt,char$
  5.   qte$=chr(34)
  6.   OutString$ = ""
  7.   Ccnt = 0
  8.   while char$<>qte$ and eof(stream)=0
  9.     Ccnt = Ccnt + 1
  10.     byt= readbyte(stream)
  11.     char$=chr(byt)
  12.   wend
  13.   char$=""
  14.   while char$<>qte$ and eof(stream)=0
  15.     Ccnt = Ccnt + 1
  16.     byt= readbyte(stream)
  17.     char$=chr(byt)
  18.     if char$ <> qte$ then OutString$=OutString$+char$
  19.   wend
  20.  
  21.   return OutString$
  22. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal