Jump to content

FileReadLine issue (among other things)


Recommended Posts

Q1: I'm having some trouble with what seems to be a basic function. FileReadLine().

It's not that I can't get it to read the file or line, i'm having a problem with one of it's return values.

Success: Returns a line of text.

Special: Sets @error to -1 if end-of-file is reached.

Failure: Sets @error to 1 if file not opened in read mode or other error.

The "Special" part is what i'm after.

I would like it to actually say when the file is over.

Here's a little test i conducted.

For $x=1 To 5
    $r=FileReadLine("testext.txt",$x)
    MsgBox(1,"",$r)
    MsgBox(1,"",@error)
Next

testext.txt
---
line 1
line 2
line 3
line 4
line end

Now, According to the help file, upon reaching the end of the file, @error should turn into -1. It didn't happen here.

the second MsgBox() always said 0

Here's an actual loop from my current project

For $cfL=1 To $ccFC[1] Step 1
            $ccFRL1=FileReadLine($gVerbPath&"_CAT.txt",$cfL)
            If FileExists($gVerbPath&$ccFRL1) Then
                $ccCF=$ccCF+1
            Else
                MsgBox(4096,"JBT: Error","Error: Missing file"&$gVerbPath&$ccFRL1)
            EndIf
        Next
    Else

What I want to do is have that dump $ccCF into a file upon reaching the end of the file. A noble cause, i believe.

Score a kill to jchd!

Q2.1 I have a bunch of spare PC's (which are broken), and i hope to combine their parts to make at least one that is somewhat working.

The one I'm going to build is going to be used as a substitute for a SNES. I have what seems to be the complete collection of snes roms and ZSnes as an emulator. I wrote a fancy looking gui to operate the thing and i'm going to buy a couple of SNes controllers to complete the experience.

I want to use said controllers to havigate the menu aswell. While i can use an ugly cluster of HotKeySet()'s to map my keyboard, I can't do so with the controller. Since I couldn't find a GetEverything() type of function in the help file, is there a UDF i can take apart to get the input from SNes controllers?

Q2.2 My fancy GUI is very Windows-y. I don't want it to be Windows-y!

I can use GuiSetBKColor() to change the backgrounds and that's about it.

The buttons seem to stay the same color.

How do I Change the color of the buttons?

If I use GuiCtrlSetImage(), Can I use .bmp's? The examples always seem to be using .dll's

Q3 A not-very-long time ago I asked how to make my thing load before explorer.exe

I got what seems to be a legit suggestion to edit a registry file. I think can find the aformentioned thread (assuming it's not too old)

But if i can't, i would love someone to post what key to edit here.

The problem with the answer in that thread however, is that I have to put my file in the same folder as explorer.exe and i would prefer not to.

Can i edit that reg key to an actual PATH to my proggy?

Edited by GodForsakenSoul
Link to comment
Share on other sites

Ask yourself: how does FileRead[Line] detect EOF condition?

When you answer this precisely, you have a clear answer to Q1.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

EOF = End of File (last character was read). As you can read here, @error will be set to -1 in this case.

I made a testscript and it works perfectly well. The file I use (test.txt) consists of:

line1
line2
line3

This script simply reads each line, displays it and stops when EOF is reached:

$file = FileOpen("test.txt", 0) ; Open for reading
$line = FileReadLine($file)
While @error <> -1
  MsgBox(0,0, $line)
  $line = FileReadLine($file)
WEnd
MsgBox(0,0, @error) ; @error = -1 here
FileClose($file)

~ edit

I already see what goes wrong. You assume EOF is reached UPON reading the last line. However, this is not the case. Once the last line is read, @error is ofcourse still 0 as there were no errors. If you try to read a line AFTER having read the last line, @error will be -1 as there are no more characters to read. If you would loop from 1 To 6 in your initial example @error would be -1 with $x = 6

Edited by d4ni
Link to comment
Share on other sites

  • Developers

@jchd: I have asked myself: How does FileReadLine detect EOF condition?

My answer is: I have no idea.

Nor do i know what's EOF, actually...

I couldn't find FileReadLine in the include files, and even if i could, i doubt i could understand the principles behind it.

What about you finally start using the Helpfile and start figuring things out yourself in stead of posting all simple questions here?

now..won't that be a great solution?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

@Jos

If all people would do that 90%+ of the topics in General Help and Support would not have to be started in the first place :mellow:

I understand and have no problem what so ever that people, in the beginning, post simple questions to get started but this friend is here long enough and has been told several times that the helpfile is there to help him. I clearly have lost my patience with him and thus will chase his ass until he learns.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@Jos: I'm beginning to think you have something against me because i visit this area quite alot.

Have YOU read the help file? Not the online one, the one that came with Scite 1.79 Jul 16 2009 18:30:31?

I'm assuming you have because your name is there. You may have also contributed to writing the help file itself. Maybe even that particular part.

Parameters

filehandle/filename The handle of a file, as returned by a previous call to FileOpen. Alternatively you may use a string filename as the first parameter. 
line [optional] The line number to read. The first line of a text file is line 1 (not zero), last line is -1. 

 

Return Value

Success: Returns a line of text. 
Special: Sets @error to -1 if end-of-file is reached. 
Failure: Sets @error to 1 if file not opened in read mode or other error. 

 

Remarks

Returns the text of the line read, any newline characters ( CHR(10) or @LF ) at the end of a line read in are automatically stripped.
If no line number to read is given, the "next" line will be read. ("Next" for a newly opened file is initially the first line.)
If a filename is given rather than a file handle - the file will be opened and closed during the function call - for parsing large text files this will be much slower than using filehandles.
Note: Do not mix filehandles and filenames, i.e., don't FileOpen a file and then use a filename in this function. Use either filehandles or filenames in your routines, not both!

From a performance standpoint it is a bad idea to read line by line specifying "line" parameter whose value is incrementing by one. This forces AutoIt to reread the file from the beginning until it reach the specified line.

Both ANSI and UTF16/UTF8 text formats can be read - AutoIt will automatically determine the type.

Maximum of 65534 characters can be return at a time.

Now, do you see ANYWHERE mentioning how it finds out that line x is the end of the file?

And let's google EOF, shall we?

Here's a selected few from the very first page.

End-of-file - Wikipedia, the free encyclopedia

In computing, end-of-file, commonly abbreviated EOF, is a condition in a computer operating system where no more data can be read from a data source. ...

---

Educational Opportunity Fund

27 May 2009 ... The Educational Opportunity Fund is one of the nation's most comprehensive and ... The EOF assists low-income residents who are capable and ...

www.state.nj.us/highereducation/EOF/index.html

---

EOF Eligibility Information

To participate in an EOF program, students must be New Jersey residents and must ... Because EOF is a campus-based program, each campus is responsible for ...

www.state.nj.us/highereducation//EOF/EOF_Eligibility.htm

---

And afterwards it's a EOF C++ search which i didn't ask for.

So, as you can see, it could be ANYTHING.

And if you care, i DO try to figure stuff out before i post here. I conduct every single test i can think of based on how much i know about that function. Sometimes i learn more, sometimes i don't, but that's not the point here.

I do my freaking best to solve my problems myself. You think I WANT to steal people's ideas?!

Or maybe you think that because i sometimes like clarification on things because i would like to make sure it doesn't jerk in an unpredicatble way

is perhaps a bad thing?

Show me the part of the TOS that say i'm not allowed to ask alot of questions on topics that YOU, the mighty and all powerful autoit creator find easy and simple.

Yeah, I'm a goddamn noob. Sue me.

Edited by GodForsakenSoul
Link to comment
Share on other sites

  • Developers

Read the AUTOIT3 helpfile when you need information on AutoIt3... The SciTE4AutoIt3 helpfile will tell you something about... SciTE4AutoIt3.

Just press F1 on any function in SciTE and it will open the helpfile and tell you all you need to know.

Do you now understand why I have an issue with you and the way you ask your questions?

They have been Over the top as to wording and too simple clearly indicating you haven't spend even a second figuring out the basics.

Well its now time to learn!

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I work with the helpfile on constantly.

I check it more then anything else.

So yeah, i'm abit "over the top" as you say. Now tell me, how "over the top" i was this time?

After reading a drunken post of mine ranting on how i couldn't get some simple (i admit) think to work, I admit i was wrong to word it the way i did.

Now from what i gather, after that particular post, you expect me to be an angry drunk poster. You also admitted you're going to "chase my ass" untill i learn.

Fun fact: For the last 5 (including today) days i've been trying to make me a .dll. Since the help file is constantly referring to them as source of pictures, sounds, windows, anything imagineable, i want to make one for my stuff, store them there instead of raw files. Not use some windows thing, MAKE MINE FROM SCRATCH.

Did you hear a peep from me on the topic?

Just because a user posts alot, doesn't mean he doesn't learn. Every single one of my posts got me some valuable info.

Every time i start with a new section of the help file, i will undoubtetly post more questions on a different topic.

So yeah, a big thing for me right now is managing files. So excuse me for being a slow learner.

Link to comment
Share on other sites

  • Developers

I work with the helpfile on constantly.

I check it more then anything else.

So yeah, i'm abit "over the top" as you say. Now tell me, how "over the top" i was this time?

Nope, because when that would have been the case other measures would have been applied as promised.

This time it was about "not knowing how to check for EOF".

It is clearly indicated in the helpfile and the example shown. So I can only conclude that you didn't read it as I assume you are clever enough to understand what is written.

Now tell me where my logic is wrong or where I made a mistake in it?

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@GodForsakenSoul

The helpfile clearly states that @error is set to -1 when end-of-file is reached, it doesn't even use the abbreviation EOF. EOF was introduced to this thread by jhcd. Besides, knowing it had something to do with reading files it is pretty damn obvious EOF stands for end-of-file.

Now, as for

Now, do you see ANYWHERE mentioning how it finds out that line x is the end of the file?

Maybe it's just me, but if line x was the last line before the end-of-file (@error = -1) notice, then x is the last line. You don't expect the help file to list all possible uses of it right? The helpfile clearly explains everything it returns and does, and includes examples to help you along but technically only the description of parameters and return values should be enough. Edited by d4ni
Link to comment
Share on other sites

I know how the function is supposed to tell me it's the end of the file.

According to the help file, @error is set to -1. I've done some testing and reached the conclusion i'm doing something wrong, went here and posted about one of my many issues. JCHD told me to check HOW THE FUNCTION knows when it's the end of the file.

That, in turn, gave you some extra ammo because i didn't quite understand what he ment by EOF. Like some people thing that "LOL" means "lots of love".

Google revealed to me 4 possibilities, 2 of which were completely impossible, however, it made me consider that EOF might mean something other then "end of file"

back to the help file.

the help file only says that the function reads the file, somehow reaches the conclusion that line x is the end of the file and changes the @error to -1.

the help file doesn't say HOW the function knows line x is the end of file, it just says that it changes @error to -1 when it finds the end of file.

jhcd asked me if i know how it does that, i said no.

i don't expect the helpfile to list all the possible uses, that would be most likely impossible.

however, since i recreated the circumstances, but failed to recreate the results, and the help file doesn't provide any more help, then it's forum time. Surprisingly, it happens alot more then i want it to.

Link to comment
Share on other sites

  • Moderators

GodForsakenSoul,

I am unsure whether you have yet grasped how FileReadLine knows it has reached the end of the file, so I will try and explain. If I have misunderstood, and you are quite content, then please excuse me and ignore this post. :mellow:

FileReadLine tries to read the line of the file you pass it as a parameter. Note that if you do not pass a parameter, it starts at the first line and then automatically increases each time you call it - nice touch that I think.

Each time FileReadLine successfully reads a line of the file it returns the line and @error is set to 0. If it fails to read the line, this could be for 2 reasons; the first (and the most likely) is Special, telling you that you have reached the end of the file and there are no more lines to read - in this case @error is set to -1; the second is Failure, basically anything else - and here the error is set to 1.

Let us look at what happens when we read a short file and see what happens - we do NOT set a prior limit on the number of times we will call FileReadLine, we just let it run:

Line 1 - FileReadLine will return "Line 1" and @error will be 0
Line 2 - FileReadLine will return "Line 2" and @error will be 0
Line 3 - FileReadLine will return "Line 3" and @error will be 0
       - Aha! No more lines - FileReadLine will return nothing and @error will be set to -1

The trick is not to set a limit on the number of times you call FileReadLine to pass through the file - you just wait until @error is set to -1. And do not forget, as was pointed out earlier, that you MUST test for @error immediately after the comand that might have set it or the value will be overwritten by the next command.

I hope that is clear. :(

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

"HOW the function knows line x is the end of file"

Line x is never the end of the file. The end of file condition is met and @error set to -1 when the FileReadline() function attempts to read the the next line and there isn't one to be read.

For example if your file contains 5 lines the end of file condition is met when you try to read line 6 or greater, not when line 5 is read which is the last line in the file.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

  • Developers

I know how the function is supposed to tell me it's the end of the file.

According to the help file, @error is set to -1. I've done some testing and reached the conclusion i'm doing something wrong, went here and posted about one of my many issues. JCHD told me to check HOW THE FUNCTION knows when it's the end of the file.

That, in turn, gave you some extra ammo because i didn't quite understand what he ment by EOF. Like some people thing that "LOL" means "lots of love".

Google revealed to me 4 possibilities, 2 of which were completely impossible, however, it made me consider that EOF might mean something other then "end of file"

back to the help file.

the help file only says that the function reads the file, somehow reaches the conclusion that line x is the end of the file and changes the @error to -1.

the help file doesn't say HOW the function knows line x is the end of file, it just says that it changes @error to -1 when it finds the end of file.

jhcd asked me if i know how it does that, i said no.

i don't expect the helpfile to list all the possible uses, that would be most likely impossible.

however, since i recreated the circumstances, but failed to recreate the results, and the help file doesn't provide any more help, then it's forum time. Surprisingly, it happens alot more then i want it to.

Again you try to come up with bullshit excuses and prove I am right in my conclusions: Do you have any idea how many explanations you will get when typing the following search argument in Google: "meaning of EOF" ?

anyway: Start showing progress and you will find I let you do your thing and might even help out every now and then.

I am done with this discussion trying to explain my issues with you as I am still convinced you are just lazy and not stupid.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Geez, I'm really sorry to see that by using the EOF acronym I would indirectly provoke a useless flamewar.

To get back to the point, my aim was that you, GodForsakenSoul, would make the following experiment: I hand you the string "ABC" and say I will ask you to give me successive letters in this string. You can either give me a new letter or tell me that there is no more available. Ready? Go!

jchd) GodForsakenSoul, give me the next letter, please.

GodForsakenSoul) Here it is: "A"

jchd) Thanks you. GodForsakenSoul, give me the next letter, please.

GodForsakenSoul) Here it is: "B"

jchd) Thanks you. GodForsakenSoul, give me the next letter, please.

GodForsakenSoul) Here it is: "C"

jchd) Thanks you. GodForsakenSoul, give me the next letter, please.

GodForsakenSoul) Sorry, jchd, there is no more letters!

jchd) Fine.

GodForsakenSoul, you now understand why you need to try to read past the last element of a string or file to discover that there is no more element to read. Doing both means that when you read a letter and before you hand it to me, you also try to read the next one just to see if there is one. We just can't always know in advance that the last element read will be the last (note the future here). Your file could be stdin as well, with a user typing letters on the fly. When the read function picks a letter, it can't read the mind of the operator to determine if (s)he will be typing ^Z (Ctrl Z is DOS EOF) the next time. Computers can't read human mind and can't time travel!

There is another good reason why it is so. As a function; only one meaningfull result is required. What I mean is that if you allow a reading function to return the last element and an EOF condition at the same time, then you have a very ambiguous function, which you will see is much more difficult to use.

Depending on what you do first (test error code or process element), you end up "forgetting" to process the last element. Given that typically in AutoIt like in most other languages around, the error condition is placed is a special language-wide variable (@error in AutoIt) which is volatile in the sense that it will be overwriten by subsequent use of another function.

Imagine you (still painted as the reading function) return both a read element and the EOF (there is no more element after this one) condition. If I test @error first, we have seen that I'll typically exit the reading loop, omitting to process the last read element. So I need to process the element first and postpone testing @error until finished. But then, @error is very likely to be overwritten by some function I'll be calling while processing the element. So I need to anticipate this and save @error in an ad hoc variable for later test. This is impractical as you see, and extremelly error-prone so that it's generally avoided if possible.

What all this (putting aside exchanges of pleasant words that arose in this thread) is teaching you is that:

-) EOF is End Of File

-) EOF isn't a "real" error, it's just an indication

-) EOF is raised when trying to read past the last element

-) you should arrange your code to test for errors right after a call because @error is "volatile" (not in the C sense of course)

-) in your own code make reading-like function work the same way and never return a valid element and an end-of-whatever condition at the same time

-) Your first example was using a For $i = 1 to 5 loop, which means you "knew" there were only 5 lines in your file. You almost never know that in advance, so that slightly cheating and is a bad practice.

I hope this all makes sense to you.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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