Jump to content

Reading From a File


Recommended Posts

I seem to be having a problem trying to read a file from start to finish one character at a time.

$File = FileOpen("Test.txt", 0)
Dim $Var = 0
While $Var = 0
    $Character = FileRead($File, 1)
    $Var = @error
    ;If @error = -1 Then ExitLoop
    ConsoleWrite($Character)
WEnd
FileClose($File)
Exit

This code works but still continues to print out rubbish even after getting all the info from the file.

My Test.txt file contains the text "This is some text" and the above script prints out some extra weird characters at the end.

Although it seems to work when I uncomment that one line that is not how I want to set my loop up.

Link to comment
Share on other sites

$File = FileOpen("Test.txt", 0)
Dim $Var = 0
While $Var = 0
    $Character = FileRead($File, 1)
    If @error = -1 Then ExitLoop
    $Var = @error
    If @error = -1 Then ExitLoop
    ConsoleWrite($Character)
WEnd
FileClose($File)
Exit

There, you get to check if the FileRead failed and then you get to check if the $Var = @error failed.

Your problem is that the If @error = -1 Then ExitLoop checks to see if the script is at the end of the file - Without it, it'll keep reading rubbish.

P.S. - I don't see what $Var = @error does, you shouldn't need it.

Edited by JoshDB
Ha, I haven't been on these forums since... 2006, almost. Behold, my legacy signature:My AutoIt idol is Valuater. You know you love him, too.My Stuff: D&D AGoT Tools Suite
Link to comment
Share on other sites

I know I could just check if FileRead failed but technically shouldn't this loop work?

Edit: Wait just understood what you ment, don't worry im just an idiot, thanks for the help.

Edited by Emperor
Link to comment
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
 Share

  • Recently Browsing   0 members

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