JohnDoe9000 Posted March 28, 2015 Share Posted March 28, 2015 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) Link to comment Share on other sites More sharing options...
l3ill Posted March 28, 2015 Share Posted March 28, 2015 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) My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
l3ill Posted March 28, 2015 Share Posted March 28, 2015 (edited) Here it is with consolewrite & Line count #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 March 28, 2015 by l3ill My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
JohnDoe9000 Posted March 28, 2015 Author Share Posted March 28, 2015 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. Link to comment Share on other sites More sharing options...
l3ill Posted March 28, 2015 Share Posted March 28, 2015 (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 March 28, 2015 by l3ill My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 28, 2015 Moderators Share Posted March 28, 2015 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
JohnDoe9000 Posted March 28, 2015 Author Share Posted March 28, 2015 (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 March 28, 2015 by JohnDoe9000 Link to comment Share on other sites More sharing options...
JohnDoe9000 Posted March 28, 2015 Author Share Posted March 28, 2015 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. M23Where can I duscuss this? It seems like I can't send PMs. Link to comment Share on other sites More sharing options...
water Posted March 28, 2015 Share Posted March 28, 2015 You have to have at least 5 or 10 posts before PM is enabled. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 28, 2015 Moderators Share Posted March 28, 2015 JohnDoe9000,I have enabled your PM facility - ready to discuss when you are. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
l3ill Posted March 28, 2015 Share Posted March 28, 2015 (edited) 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 However this works: While @error = 0 Edited March 28, 2015 by l3ill My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
Solution JohnDoe9000 Posted March 28, 2015 Author Solution Share Posted March 28, 2015 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now