Write and Read from a specific line of a file ?

Just starting out? Need help? Post your questions and find answers here.
Walko
User
User
Posts: 11
Joined: Thu Mar 26, 2020 6:08 pm

Write and Read from a specific line of a file ?

Post 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 :)
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

If you want all lines, better use a list then an array.
Because then you don't reach a limit.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

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

Post 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
sorry for my bad english
Walko
User
User
Posts: 11
Joined: Thu Mar 26, 2020 6:08 pm

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

Post 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:
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post by IdeasVacuum »

... you have definitely chosen the best programming language and what do you know - it comes with the best forum!
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply