Jump to content
JohnDoe9000

Reading text files, testing @error

Recommended Posts

JohnDoe9000

What is the proper way to read text files line by line?

Why the following code doesn't detect end of file?

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Local $sInputFileName = @ScriptDir & "\somefile.xml"
Local $LineCount = 0
Local $CharCount = 0

$hInFile = FileOpen($sInputFileName, $FO_READ)

While Not @error
   $sLine = FileReadLine($hInFile)
   If @error Then ExitLoop
   $LineCount += 1
   $CharCount += StringLen($sLine)
WEnd

If @error Then
   MsgBox($MB_OK + $MB_ICONERROR, "File processing", "An error occured when reading the file" & @CRLF & $sInputFileName)
Else
   MsgBox($MB_OK + $MB_ICONINFORMATION, "File processed", "File" & $sInputFileName & @CRLF & "contains " & $LineCount & " lines and " & $CharCount & " characters.")
EndIf

FileClose($hInFile)

Share this post


Link to post
Share on other sites
l3ill

Try this

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Local Const $sInputFileName = @ScriptDir & "\somefile.xml"
Local $LineCount = 0
Local $CharCount = 0

$hInFile = FileOpen($sInputFileName)


While 1
    $line = FileReadLine($hInFile)
    If @error = -1 Then ExitLoop
;~     MsgBox(0,'',$line)
WEnd
FileClose($hInFile)


If @error Then
   MsgBox($MB_OK + $MB_ICONERROR, "File processing", "An error occured when reading the file" & @CRLF & $sInputFileName)
Else
   MsgBox($MB_OK + $MB_ICONINFORMATION, "File processed", "File" & $sInputFileName & @CRLF & "contains " & $LineCount & " lines and " & $CharCount & " characters.")
EndIf

FileClose($hInFile)

If your are looking for line count you will need this:

_FileCountLines($file)

Share this post


Link to post
Share on other sites
l3ill

Here it is with consolewrite & Line count  o:)

#include <FileConstants.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Local Const $sInputFileName = @ScriptDir & "\somefile.xml"
Local $LineCount = 0
Local $CharCount = 0

$hInFile = FileOpen($sInputFileName)


While 1
    $line = FileReadLine($hInFile)

    If @error = -1 Then ExitLoop
    $LineCount = _FileCountLines($sInputFileName)
    ConsoleWrite($line & @CRLF)
WEnd
FileClose($hInFile)


If @error Then
    MsgBox($MB_OK + $MB_ICONERROR, "File processing", "An error occured when reading the file" & @CRLF & $sInputFileName)
Else
    MsgBox($MB_OK + $MB_ICONINFORMATION, "File processed", "File" & $sInputFileName & @CRLF & "contains " & $LineCount & " lines and " & $CharCount & " characters.")
EndIf

FileClose($hInFile)
Edited by l3ill

Share this post


Link to post
Share on other sites
JohnDoe9000

Thank you for quick reply.

I'm afraid your variant won't catch real I/O errors, both while opening the file and reading lines.

Also I want to undestand how "If @error" actually works. I expected it to be true when @error is nonzero.

And line count here is just for example.

Share this post


Link to post
Share on other sites
l3ill

N.P.

@error reacts to the last called function and is different depending on the function (see help file)

For instance:

FileReadLine

Success: a line of text.

Failure: sets the @error flag to non-zero.

@error:

 1 = if file not opened in read mode or other error
-1 = if end-of-file is reached

In this case non zero just means your function didn't fire for some reason.

also setting If -1 exit loop makes your function end when the EOF is reached.

edit:

if you want error handling for FileOpen then insert one thereafter

Edited by l3ill

Share this post


Link to post
Share on other sites
Melba23

Hi,

I have changed your forum display name as it was not at all suitable. When you choose a new name, please try and pick something that does not so obviously flout the ethos of the forum - which is clearly set out in the >Forum rules. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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
JohnDoe9000

if you want error handling for FileOpen then insert one thereafter

 

I thought my original "While Not @error" provided one, didn't it?

Edited by JohnDoe9000

Share this post


Link to post
Share on other sites
JohnDoe9000

Hi,

I have changed your forum display name as it was not at all suitable. When you choose a new name, please try and pick something that does not so obviously flout the ethos of the forum - which is clearly set out in the >Forum rules. :)

M23

Where can I duscuss this? It seems like I can't send PMs.

Share this post


Link to post
Share on other sites
water

You have to have at least 5 or 10 posts before PM is enabled.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Melba23

JohnDoe9000,

I have enabled your PM facility - ready to discuss when you are. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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
l3ill

 

I thought my original "While Not @error" provided one, didn't it?

Not sure... never seen (While | Wend)  used like that in AutoIT

Not to mention... it wasn't working   :ermm:   ;)

However this works:

While @error = 0
Edited by l3ill

Share this post


Link to post
Share on other sites
JohnDoe9000

It tuns out that FileOpen() doesn't set @error. I missed that point in documentation, looking at other file I/O functions that do set it.

And more, ConsoleWrite() do set it (or clear in case of success), while documentation doesn't mention this fact. Such inconsistency is really misleading.

The final code that works as intended:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Local $sInputFileName = @ScriptDir & "\1.txt"
Local $LineCount = 0
Local $CharCount = 0
Local $error = 0

$hInFile = FileOpen($sInputFileName, $FO_READ)

While $hInFile <> -1
   $sLine = FileReadLine($hInFile)
   $error = @error
   If $error <> 0 Then ExitLoop
   $LineCount += 1
   $CharCount += StringLen($sLine)
WEnd

If $error <> -1 Then
   MsgBox($MB_OK + $MB_ICONERROR, "File processing", "An error occured when reading the file" & @CRLF & $sInputFileName)
Else
   MsgBox($MB_OK + $MB_ICONINFORMATION, "File processed", "File" & $sInputFileName & @CRLF & "contains " & $LineCount & " lines and " & $CharCount & " characters.")
EndIf

FileClose($hInFile)

Thanks for participation.

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

×