CSV file help

Just starting out? Need help? Post your questions and find answers here.
Rikuk
User
User
Posts: 24
Joined: Mon May 30, 2005 11:36 am

CSV file help

Post by Rikuk »

Hi I'm currently loading a CSV file with each string seperated by "," and using a text gadget to display the text.

This is working fine with the CSV dile formated as follows

STRING 1,STRING 2,STRING 3,STRING 4

However the data I going to be using is formated as follows

STRING 1,STRING 2
STRING 3,STRING 4

When I load the CSV file formated like this the last two string only are displayed in the first two text gadgets only

Could someone please correct the code below or explain why this is happening.

Thanks

RIK :oops:
***************CODE**********************

If OpenWindow(0,0,0,400,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"TEST")
CreateGadgetList(WindowID(0))
If ReadFile(0,"c:\TEST.txt")
Repeat
TEXT$=ReadString()
Until Eof(0)
CloseFile(0)
EndIf
Dim TEXT$ (4):X=8
For I=1 To 4
TEXT$ (I)=StringField(TEXT$,I, ",")
StringGadget(I,8,X,306,20,TEXT$(I),#PB_String_UpperCase)
X=X+20
Next
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

With your format as this:

Code: Select all

STRING 1,STRING 2,
STRING 3,STRING 4
You could do it by changing the = to an + operator

As it follows:

Code: Select all

If OpenWindow(0,0,0,400,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"TEST")
  CreateGadgetList(WindowID(0))
  If ReadFile(0,"c:\TEST.txt")
    Repeat
      TEXT$+ReadString()
    Until Eof(0)
    CloseFile(0)
  EndIf
  Dim TEXT$ (4):x=8
  For I=1 To 4
    TEXT$ (I)=StringField(TEXT$,I, ",")
    StringGadget(I,8,x,306,20,TEXT$(I),#PB_String_UpperCase)
    x=x+20
  Next
  Repeat:Until WaitWindowEvent()=#pb_event_closewindow
EndIf

Now, If you want to use the format you did show up in here, then this would do:

Code: Select all

If OpenWindow(0,0,0,400,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"TEST")
  CreateGadgetList(WindowID(0))
  If ReadFile(0,"c:\TEST.txt")
    Repeat
      TEXT$+ReadString()+"," ; we add our , by code
    Until Eof(0)
    CloseFile(0)
  EndIf
  
  Dim TEXT$ (4):x=8
  For I=1 To 4
    TEXT$ (I)=StringField(TEXT$,I, ",")
    StringGadget(I,8,x,306,20,TEXT$(I),#PB_String_UpperCase)
    x=x+20
  Next
  Repeat:Until WaitWindowEvent()=#pb_event_closewindow
EndIf

The method you're using is not the best at all, but if it works for you now, and you're quite new in programming, I suggest you to keep it until you're a little more confident to try new things.

Another thing: As you're using version 3.x, keep in mind that the string buffer size is fixed (to 64000 bytes). In 4.x the size is not fixed, so you wont find the limitation.

Could someone please correct the code below or explain why this is happening
You were asigning a new string on each pass of your loop! thats the explaination, you should use the + operator, which will ADD to your existant string.

Since you had multiple lines, only the last one was being shown up because it was the last one read from the file.


Now.. since you're using an array (you size it as 4 but it should be 3 as 0 counts as 1 though) you should read into the string array instead and then use it to display the data. Using a string here is no help, better is to have a string array.

Code: Select all

If OpenWindow(0,0,0,400,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"TEST")
 
  If ReadFile(0,"c:\test1.txt")
    ArraySize.l = 3;
    Dim TEXT.s (ArraySize)
    
    Repeat
      tempStr.s =ReadString()+","
      I+1
      Count = CountString(tempStr,",")-1
      For c=0 To Count
        TEXT(CountOffset) = StringField(tempStr,c+1,",")
        CountOffset+1
      Next
    Until Eof(0)
    CloseFile(0)
  
    CreateGadgetList(WindowID(0))
    x.l =8
    
  For I=0 To ArraySize;
    tempStr.s =TEXT(I)
    StringGadget(I,8,x,306,20,tempStr,#PB_String_UpperCase)
    x=x+20
  Next
  
Else
  MessageRequester("error","Could not Read the file",#MB_OK|#MB_ICONERROR);
EndIf
Repeat:Until WaitWindowEvent()=#pb_event_closewindow
EndIf
But I wouldnt be using ReadString() in the first place..



Have fun.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Rikuk
User
User
Posts: 24
Joined: Mon May 30, 2005 11:36 am

Post by Rikuk »

Thank you for your reply :D
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

No problem, bring them on!.
And don't forget to keep an eye on the manual, that helps!.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Post Reply