Page 1 of 1

Write and Read from a specific line of a file ?

Posted: Thu Mar 26, 2020 6:17 pm
by Walko
Hello,

Im a beginner in Purebasic...
I would like to create a program/GUI to communicate with a device connected to a com port.
I found an example to handle com port but I would like to create a file to store previous settings ( com port number, baurate, etc ).

Is there a way to write and/or read on a specific line of a file ?
For exemple, if I have the com port number on line 3 in a .txt file, how to read just the line 3 ?

Thank you for your help :)

Re: Write and Read from a specific line of a file ?

Posted: Thu Mar 26, 2020 6:38 pm
by IdeasVacuum
Hi

You could just read but ignore lines 0, 1 and 2. Or you could read the file into an array, if you know how many lines are in the file.

Something like this:

Code: Select all

Procedure.s ReadMyFile(sMyFile.s)
;#-------------------------------
Protected Dim sFileLine.s(20)   ;Max 21 lines in file including line zero
Protected iCnt.i, iTotal.i = 21

               If ReadFile(#FileIO, sMyFile, #PB_UTF8)

                         For iCnt = 0 To iTotal

                                   sFileLine(iCnt) = ReadString(#FileIO, #PB_UTF8)
                                   If(iCnt = 3) : Break : EndIf
                         Next

                         CloseFile(#FileIO)

                         ProcedureReturn(sFileLine(3))
               Else
                         MessageRequester("Error", "Could not read File", #PB_MessageRequester_Ok | #PB_MessageRequester_Info)
                          ProcedureReturn("Error")
               EndIf
EndProcedure
Assumption:
You are not writing a BOM (Byte Order Mark) to indicate the String encoding used

Edit: Added a Break in the loop so Read stops once line 3 has been read

Re: Write and Read from a specific line of a file ?

Posted: Thu Mar 26, 2020 6:52 pm
by IdeasVacuum
And:

Code: Select all

Procedure OverWriteMyFile(sMyFile.s, sNewLine3.s)
;#-----------------------------------------------
Protected Dim sFileLine.s(20)   ;Max 21 lines in file including line zero
Protected iCnt.i, iTotal.i = 21

               If ReadFile(#FileIO, sMyFile, #PB_UTF8)

                         For iCnt = 0 To iTotal

                                   sFileLine(iCnt) = ReadString(#FileIO, #PB_UTF8)
                         Next

                         CloseFile(#FileIO)

                         sFileLine(3) = sNewLine3

                         If CreateFile(#FileIO, sMyFile, #PB_UTF8)

                                   For iCnt = 0 To iTotal
                                   
                                             WriteStringN(#FileIO, sFileLine(iCnt), #PB_UTF8)
                                   Next
                                   
                                   CloseFile(#FileIO)
                         EndIf
               Else
                         MessageRequester("Error", "Could not read File", #PB_MessageRequester_Ok | #PB_MessageRequester_Info)
               EndIf
EndProcedure

Re: Write and Read from a specific line of a file ?

Posted: Thu Mar 26, 2020 7:33 pm
by infratec
If you want all lines, better use a list then an array.
Because then you don't reach a limit.

Re: Write and Read from a specific line of a file ?

Posted: Thu Mar 26, 2020 7:39 pm
by Josh
Walko wrote:... but I would like to create a file to store previous settings ( com port number, baurate, etc )
Have a look to the library 'Preference'. This is made to save such settings.

https://www.purebasic.com/documentation ... index.html

Re: Write and Read from a specific line of a file ?

Posted: Sat Mar 28, 2020 3:45 pm
by Walko
Thank you very much !
I didn't expect such an active forum !
I understand almost nothing, I will study your example with google and purebasic documentation :oops:

Re: Write and Read from a specific line of a file ?

Posted: Sat Mar 28, 2020 5:07 pm
by IdeasVacuum
... you have definitely chosen the best programming language and what do you know - it comes with the best forum!