Barfly Posted August 18, 2005 Share Posted August 18, 2005 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. 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) Link to comment Share on other sites More sharing options...
Barfly Posted August 18, 2005 Author Share Posted August 18, 2005 Hi I have gotten a little further, but the script only picks up that the EOF has been reached during the second loop. 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? Link to comment Share on other sites More sharing options...
LxP Posted August 18, 2005 Share Posted August 18, 2005 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... Link to comment Share on other sites More sharing options...
Barfly Posted August 18, 2005 Author Share Posted August 18, 2005 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? Any ideas? Link to comment Share on other sites More sharing options...
LxP Posted August 18, 2005 Share Posted August 18, 2005 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? Link to comment Share on other sites More sharing options...
Barfly Posted August 18, 2005 Author Share Posted August 18, 2005 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? $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. Link to comment Share on other sites More sharing options...
LxP Posted August 18, 2005 Share Posted August 18, 2005 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? Link to comment Share on other sites More sharing options...
Barfly Posted August 18, 2005 Author Share Posted August 18, 2005 I could kiss you!!!!! So simple...is a perfect solution! Thanks you so much! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now