Jump to content

reading a text file


cbroui
 Share

Recommended Posts

I want to read a line of text out of a text file. I have tried opening a text file copied straight from the help file and it also fails to open. The file does exist, I have tried with the file in the same directory and hard coding a path. What am I missing?

CODE
$file = FileOpen("test.txt", 0)

FileOpen("test.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

; Read in lines of text until the EOF is reached

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

MsgBox(0, "Line read:", $line)

Wend

FileClose($file)

I have a newly created test.txt file in the same directory. I have also tried this on 3 separate WinXP machines and it always returns "Unable to open file" msg.

Link to comment
Share on other sites

I want to read a line of text out of a text file. I have tried opening a text file copied straight from the help file and it also fails to open. The file does exist, I have tried with the file in the same directory and hard coding a path. What am I missing?

CODE
$file = FileOpen("test.txt", 0)

FileOpen("test.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

; Read in lines of text until the EOF is reached

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

MsgBox(0, "Line read:", $line)

Wend

FileClose($file)

I have a newly created test.txt file in the same directory. I have also tried this on 3 separate WinXP machines and it always returns "Unable to open file" msg.

You've got two FileOpen statements in your code.
Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

You've got two FileOpen statements in your code.

Even that does not stop it working for me.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

The code was copied straight out of the help file for FileReadLine. It failed and i can not understand why. So I reread the code and thought 'maybe it is not opening because I did not tell it to open. Line one assigns the open command into a variable but does not execute it.' So I added a second open statement to be sure the command to open was executed. Still no luck.

If it were just happening on one machine I would think it is my OS or policy or something local. I can not get a file to read on any machine using any command from V3. I have used Autoit V2 extensively and never had issues using any of the supplied commands. Can you copy the exact script I posted, drop it on AutoIt3.exe and the file opens successfully?

Edited by cbroui
Link to comment
Share on other sites

I just copied and pasted the text into an au3 file and ran it just fine. Are you sure the test.txt is in the same directory as your au3 file?

Yes. And I can not stress enough how aggravating this is. I can even copy the script and AutoIt3.exe to the desktop of a freshly imaged PC and create a new text document named test.txt with one line of text in it. SAME THING. I have to be doing something wrong. I have even tried creating a temp folder on the root of my drive, placed the three files in it and hardcoded the path. It refuses to open. I have downloaded a new copy of AutoIt3.exe just because I can not think of anything else to try. Same issue. I feel like I am on crazy pills.
Link to comment
Share on other sites

Hooray! I am not insane.

Apparently V3 of Autoit is not fully drag and drop compatible like V2 was. If you drag and drop the script onto your AutoIt3.exe you will reproduce my frustration. The script runs as in you get the msgbox telling you the file did not open, but the Fileopen fails. If you tell Windows to run your script with AutoIt3.exe it works as designed. Thanks for the quick responses and I hope the next person going out of their mind finds this before they give up on the new version.

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