WestCBear

FileReadLine Not Advancing Lines

6 posts in this topic

#1 ·  Posted (edited)

Supposedly, if you don't add the line number in the call to FileReadLine  (For example, FileReadLine($openedFile)), every time you call the function it will just assume the next line in the file. 

This doesn't seem to work for me.  Here is the code I use. (I'm using this function in tandem with the SQLite function.)

 

If I take out the line number in the call to FileReadLine, it reads the same line over and over. (For Example: $aString = FileReadLine($ESDFile))   However, if I put the line number back in, it reads each line properly.  I feel like I'm missing something small.

Here is the code:  Both the database and the file are opened before the "While" statement and are closed after it.

While 1
  $aString = FileReadLine($ESDFile, $b)
  If @error <> 0 Then
   ExitLoop

  EndIf

  $stringArray = StringSplit($aString, "|")
  $b = $b + 1
  GUICtrlSetData($Label1, $b)


  If StringCompare($stringArray[1], $currentCountryName) <> 0 Then
   $currentCountryName = $stringArray[1]
   _SQLite_Exec (-1,"CREATE TABLE " & $stringArray[1] & "(CountryCode text, CountryName text, StateCode text, StateName text, City text, ZipHigh text, ZipLow text);")
   _SQLite_Exec (-1,"INSERT INTO COUNTRY VALUES('" & $stringArray[1] &"','" & $stringArray[2] & "');")
  EndIf



  _SQLite_Exec (-1,"INSERT INTO " & $stringArray[1] & " VALUES('" & $stringArray[1] & "','" & $stringArray[2]& "','" & $stringArray[3]& "','" & $stringArray[4]& "','" & $stringArray[5]& "','" & $stringArray[7]& "','" & $stringArray[8] &"');")

 WEnd

 

Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites



WestCBear,

Welcome to the AutoIt forums.

Your code works fine for me - as demonstrated in this simple example:

#include <GUIConstantsEx.au3>

$ESDFile = FileOpen(@ScriptFullPath)

$hGUI = GUICreate("Test", 500, 500)

$Label1 = GUICtrlCreateLabel("", 10, 10, 40, 40)
GUICtrlSetFont(-1, 18)

GUISetState()

$b = 1

While 1

    $aString = FileReadLine($ESDFile, $b)
    If @error <> 0 Then
        ExitLoop

    EndIf

    ;$stringArray = StringSplit($aString, "|")
    $b = $b + 1
    GUICtrlSetData($Label1, $b)

    ConsoleWrite($b & " - " & $aString & @CRLF)

    Sleep(500)

WEnd



While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
  FileClose($ESDFile)
            Exit
    EndSwitch

WEnd

I get each line of the file written in the SciTE console and the label counts along. However, I would suggest not using the "line" parameter when you call FileReadLine - as explained in the Help file:

From a performance standpoint it is a bad idea to read line by line specifying "line" parameter whose value is incrementing by one. This forces AutoIt to reread the file from the beginning until it reach the specified line.

So better to leave the command as a simple:

$aString = FileReadLine($ESDFile)

and let AutoIt take care of the count for you.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hi Melba23. Thanks for the Welcome and thanks for the help!  :)

Well using the code you gave me above as a starting point, I've done some deeper digging and I've discovered that it's the FileOpenDialog function which is causing this.

When I use the FileOpenDialog function and then use the FileReadLine without using the "line" parameter, the first line of the text repeats.

I tried the code given above, but swapped

$ESDFile = FileOpen(@ScriptFullPath)

for

$ESDFile = FileOpenDialog("Choose the ESD file...", @ScriptDir, "TXT (*.txt)")

(And I took the "line" parameter out.)

Are you getting the same thing as me?

Share this post


Link to post
Share on other sites

WestCBear,

Interesting. If you pass a file handle to FileReadLine the automatic line advance works fine, but if you pass a file name you need to use the line parameter to advance.

I can see why this would be the case - using the file name causes the file to be opened and closed each time you read a line and so the count is continually reset, as the Help file suggests:

If no line number to read is given, the "next" line will be read. ("Next" for a newly opened file is initially the first line.)

But it is not made clear that the automatic advance only works when a file handle is passed. I will check with the developers that this is indeed the case and amend the Help file accordingly. Thanks for noting this discrepancy.

And to solve your problem - just open the file selected by FileOpenDialog:

$ESDFile = FileOpen(FileOpenDialog("Choose the ESD file...", @ScriptDir, "txt (*.txt)"))

Now you pass a file handle to the function and the automatic line advance should work (it does for me).

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hi Melba23

 

That works perfectly.  Thank you!  :)

 

 

Share this post


Link to post
Share on other sites

WestCBear,

Glad I could help. The Help file will now read:

Returns the text of the line read, any newline characters ( @CR or @LF ) at the end of the line are automatically stripped.

If a filehandle is passed to the function and no line number is specified, the "next" line will be read - for a newly opened file this will be the first line. If lines are read in a loop, it is recommended to let the count increase automatically and not to use the "line" parameter, as doing so will force AutoIt to re-read the file from the beginning each time, significantly slowing execution time for large files.

If a filename is passed to the function, the file will be opened and closed during the call which slows execution considerably, particularly when reading multiple lines. Furthermore, because the file is closed after each call, there is no automatic "next" line increase and the "line" parameter must be used when reading lines in a loop. Note that this will further slow execution as AutoIt re-reads the file each time.

Do not mix filehandles and filenames, i.e. do not FileOpen() a file and then use a filename in this function. Use either filehandles (recommended) or filenames in your routines, not both!

Thanks again for highlighting the matter.

M23 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now