Jump to content

Reading text files, testing @error


Go to solution Solved by JohnDoe9000,

Recommended Posts

Posted

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)
Posted

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)
Posted (edited)

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
Posted

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.

Posted (edited)

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
  • Moderators
Posted

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

 

Posted (edited)

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
Posted

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.

Posted

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

  • Moderators
Posted

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

 

  • Solution
Posted

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.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...