Sign in to follow this  
Followers 0
zvd

FileReadLine

11 posts in this topic

Hello, in the following example, exclude.txt has 5 lines of text. The message box pops up 5 times to display each line of text. How can I make just one message box pop up with all 5 lines of text on one line?

Thanks|

$file = FileOpen("c:\temp\exclude.txt", 0)
If $file = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf

While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
   MsgBox(0, "Test", $line)
WEnd
FileClose($file)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Just store each line into a variable

Global $total
$file = FileOpen("c:\temp\exclude.txt", 0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $total = $total & @CRLF & $line
WEnd

FileClose($file)

MsgBox(0, "Test", $total)

:)

Edit:

Code mistake fixed.

Edited by MHz

Share this post


Link to post
Share on other sites

You're example would only ever read the first line anyway. You never increase $line.

$sString = StringReplace(StringStripCR(FileRead("c:\temp\exclude.txt")), @LF, ' ')
MsgBox(64, 'Info', $sString)
The Carriage Returns/Line Feeds are replaced by spaces.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

$file = FileOpen("c:\temp\exclude.txt", 0)
Global $Line[6]
If $file = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf

While 1
   $Message = ""
   For $i = 1 to 5
   $line[$i] = FileReadLine($file,$i)
   $Message= $Message&$Line[$i]&@CRLF
   Next
   If @error = -1 Then ExitLoop
   MsgBox(0, "Test", $message)
WEnd
FileClose($file)

Yeah, I was beat...

Share this post


Link to post
Share on other sites

Thanks! One thing - the number of lines in the text file may change....

Share this post


Link to post
Share on other sites

Yes, I tried working with that but had the same problem - trying to get just one message box with all the text on just one line (no CRLF's).

Share this post


Link to post
Share on other sites

Yes, I tried working with that but had the same problem - trying to get just one message box with all the text on just one line (no CRLF's).

I gave you the answer then.

http://www.autoitscript.com/forum/index.ph...st&p=296180


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

You're example would only ever read the first line anyway. You never increase $line.

Not true.

If he hadn't used FileOpen but given FileReadLine the filename instead, it's true however.

When FileReadLine is given a handle it loops through the file, line by line.

$sFile = FileOpenDialog("", "", "Text (*.*)")
If @error Then Exit

$hFile = FileOpen($sFile, 0)
If @error Then Exit

While 1
    $sLine = FileReadLine($hFile)
    If @error Then ExitLoop
    
    MsgBox(64, "", StringLeft($sLine, 40) & "...")
WEnd

MsgBox(64, "", "Finished")

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Not true.

If he hadn't used FileOpen but given FileReadLine the filename instead, it's true however.

When FileReadLine is given a handle it loops through the file, line by line.

$sFile = FileOpenDialog("", "", "Text (*.*)")
If @error Then Exit

$hFile = FileOpen($sFile, 0)
If @error Then Exit

While 1
    $sLine = FileReadLine($hFile)
    If @error Then ExitLoop
    
    MsgBox(64, "", StringLeft($sLine, 40) & "...")
WEnd

MsgBox(64, "", "Finished")
I can honestly say, I didn't... I didn't even know that :) ... thanks. Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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
Sign in to follow this  
Followers 0