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
Write and Read from a specific line of a file ?
-
- 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 ?
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:
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
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
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.
If it sounds simple, you have not grasped the complexity.
-
- 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 ?
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.
If it sounds simple, you have not grasped the complexity.
Re: Write and Read from a specific line of a file ?
If you want all lines, better use a list then an array.
Because then you don't reach a limit.
Because then you don't reach a limit.
Re: Write and Read from a specific line of a file ?
Have a look to the library 'Preference'. This is made to save such settings.Walko wrote:... but I would like to create a file to store previous settings ( com port number, baurate, etc )
https://www.purebasic.com/documentation ... index.html
sorry for my bad english
Re: Write and Read from a specific line of a file ?
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
I didn't expect such an active forum !
I understand almost nothing, I will study your example with google and purebasic documentation
-
- 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 ?
... 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.
If it sounds simple, you have not grasped the complexity.