Jump to content

Read text file line by line problem after for loop

Recommended Posts

Is it possible to read line one by one after the loop?

#include <Array.au3>
#include <File.au3>

$TxtFileHandle = @ScriptDir & "\TxtFile.txt"

$ReadLines = ""
Local $Var
_FileReadToArray($TxtFileHandle, $Var)
For $i = 1 to UBound($Var) -1
    $ReadLines &= $Var[$i] & @CRLF
MsgBox(0,"", $ReadLines)


#include <File.au3>

$TxtFileHandle = @ScriptDir & "\TxtFile.txt"
$OpenFileTxt = FileOpen($TxtFileHandle, 0)
If $OpenFileTxt = -1 Then
    MsgBox(0, "Error", "Unable to open file.")

$ReadLines = ""
For $i = 1 To _FileCountLines($TxtFileHandle) Step 1
    $ReadLines &= FileReadLine($OpenFileTxt, $i) & @CRLF
MsgBox(0, "", $ReadLines)


Link to post
Share on other sites

You aren't making any sense. You combine the individual lines into an single variable and then complain when the full variable is displayed in the MsgBox. Then I show you how to do it with a For loop, and you complain that you want the MsgBox to come after the Next.

How do you expect to display the individual lines without using a MsgBox inside a For loop? Magic? 😒


Link to post
Share on other sites

Let me take another tack:

@youtuber, so you have this file, TxtFile.txt, right?

1) How many lines are in it?

2) How many times do you want msgbox to display by the time the program exits?

3) Give a example of a msgbox you EXPECT to receive when the program is working - USE REAL DATA, not variable names

4) If possible, post your TxtFile.txt

If you do all these things, either you will figure it out then, or somebody will right after.

Code hard, but don’t hard code...

Link to post
Share on other sites

Everybody and me don't understand what you want to achieve.
Below is the code I use when working with lines and large files.

Global $iFileIN = @ScriptFullPath 
Global $sNewFileContent, $sFileContent, $sLine, $Read_Line = 0, $iLineCount = 0
While 1
    $Read_Line = $Read_Line + 1
    $sLine = FileReadLine($iFileIN, $Read_Line)
    If @error <> 0 Then ExitLoop
    $iLineCount += 1
    $sFileContent &= $sLine & @CRLF
    $sNewFileContent &= _Work_On_This_Line($sLine, $iLineCount) & @CRLF


Func _Work_On_This_Line($iLine, $iLineCount)
    ; Do somethink here
    ConsoleWrite('-LINE ' & $iLineCount & ': ' & $iLine & @CRLF)
    Return $iLine
EndFunc   ;==>_Work_On_This_Line



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

    No registered users viewing this page.

  • Create New...