Jump to content
youtuber

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
Next
MsgBox(0,"", $ReadLines)

or

#include <File.au3>

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

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

 

Share this post


Link to post
Share on other sites

The $ReadLines variable does not fall into the Message box one by one after the for loop.
She's reading lines collectively :(

 

Share this post


Link to post
Share on other sites
28 minutes ago, youtuber said:

She's reading lines collectively

You’re the one “collecting” them, with your &=

$ReadLines &= FileReadLine($OpenFileTxt, $i) & @CRLF

I think you owe “her” an apology :)


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

Share this post


Link to post
Share on other sites
Posted (edited)

MsgBox(0,"", $Var[$i])
Next

I want the message box after the for loop


Next

MsgBox(0,"", $Var[$i])

Edited by youtuber

Share this post


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? 😒

 

Share this post


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...

Share this post


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
WEnd

MsgBox(0,'xXx',$sNewFileContent)

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

 


Regards,
 

Share this post


Link to post
Share on other sites
Posted (edited)

Please tell me you’re a troll...

Line1
Line2
Line3
Line4

 

Edited by JockoDundee

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

Share this post


Link to post
Share on other sites

Instead of beating a MsgBox, use a Gui ListBox that you can fill during the loop, and lines will show all along the way.

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...