Sign in to follow this  
Followers 0
Barfly

Read text to EOF using FileRead

8 posts in this topic

Hi

I feel really silly asking such a simple question, :"> but I am really stuck!

I want to be able to read from a text file, and if the EOF is reached then display a message, if not then exitloop.

I know that sounds obvious but I can only get it to work in reverse: If EOF is reached, exitloop; if not then display a message. :whistle:

If anyone can help, I'd really appreciate it!

Here is what I have so far...

$file = 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 1 character at a time until the EOF is reached
For $i 1 to 10
    FileRead($file, 100)
    If @error = -1 Then MsgBox(0, "", "You have exceeded the maximum length")
ExitLoop
    Else
Exitloop
Next

FileClose($file)

Share this post


Link to post
Share on other sites



Hi

I have gotten a little further, but the script only picks up that the EOF has been reached during the second loop. :dance: I need it to work like this: Read the file...if EOF was reached before could complete then a message appears.

For $i = 1 to 2 
FileRead($file, 2000)
    If @error = -1 Then 
        MsgBox(0, "", "End of File was reached!")
        exitloop
        EndIf
MsgBox(0, "", "End of file not reached...")
next

Help? :whistle:

Share this post


Link to post
Share on other sites

I'm not quite sure what you're trying to achieve. Are you attempting to read information from the file and then exit the loop if it failed due to reaching the end of the file?

Also, you do not appear to be catching the output of the FileRead() function...

Share this post


Link to post
Share on other sites

I'm not quite sure what you're trying to achieve. Are you attempting to read information from the file and then exit the loop if it failed due to reaching the end of the file?

Also, you do not appear to be catching the output of the FileRead() function...

<{POST_SNAPBACK}>

OK, maybe I should start at the beginning...

Basically what I need is for a user to save some text, (entered in a GUI) to a file. That is simple enough. But I want there to be a limit to the length of the text file. let's say 2000 characters. So when they click on Save, it warns them that they have exceeded this amount.

I thought that the best way to do this would be to FileRead...I could be wrong? :whistle:

Any ideas?

Share this post


Link to post
Share on other sites

If the file is being created from the user's GUI input then you could do a length check on the control data using StringLen().

If you are appending to the file you could use FileGetSize() and add the result to StringLen() to get the expected file size if the user continues.

Are either of these what you're after?

Share this post


Link to post
Share on other sites

Sort of...

Yes, the user will keep adding to the end of the .txt file, every time they save. At some point the user will want to display this file and I am limited by space at this point. So I want to get the length of the text file, so that if the user tries to save a file that will be over this limit...I can warn them.

I thought I was on the right track with my previous code...am I veryt much in the dark here? :dance:

$file = 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 1 character at a time until the EOF is reached
For $i 1 to 10
    FileRead($file, 100)
    If @error = -1 Then 
ExitLoop
    Else
MsgBox(0, "", "You have exceeded the maximum length")
EndIf
Next

FileClose($file)

It keeps complaining about the 'Else' closing the 'For' tag. :whistle:

Share this post


Link to post
Share on other sites

You're close -- just an error in your For statement:

For $i = 1 to 10
; (was missing equals sign)

Your code should definitely do what it describes, but here is another version that does the same thing in a clearer fashion:

$size = fileGetSize("test.txt")

if @error then
    msgBox(0x10, "Error", "Unable to open file.")
    exit
endIf

if ($size > 2000) then
   msgBox(0x30, "Error", "You have exceeded the maximum length.")
   exit
endIf

; (remainder of script)

Does that get you any closer?

Share this post


Link to post
Share on other sites

I could kiss you!!!!! :whistle:

So simple...is a perfect solution! Thanks you so much!

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