Code example for Dir not working [as expected] SOLVED!
Code example for Dir not working [as expected] SOLVED!
hope this is the right place to report an error in the FB chm Dir page and wiki https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDir
Edit: turns out it was me not the example!
when i was looking at the Dir command first code example (both the frebasic 1050 chm and fbwiki) i noticed that the calls to the "list_files" subroutine
list_files "*", fbDirectory
list_files "*", fbArchive
are missing parenthesis.
I added parenthesis to make the example work:
list_files ("*", fbDirectory)
Edit: turns out it was me not the example!
when i was looking at the Dir command first code example (both the frebasic 1050 chm and fbwiki) i noticed that the calls to the "list_files" subroutine
list_files "*", fbDirectory
list_files "*", fbArchive
are missing parenthesis.
I added parenthesis to make the example work:
list_files ("*", fbDirectory)
Last edited by andykmv on Mar 28, 2018 21:02, edited 1 time in total.
Re: Code example for Dir not working
Hi,
No problem without parenthesis......
What error message do you get ?
Parenthesis are not necessary when calling subs or funcs though it's (for me) cleaner/clearer.
No problem without parenthesis......
What error message do you get ?
Parenthesis are not necessary when calling subs or funcs though it's (for me) cleaner/clearer.
Last edited by SARG on Mar 16, 2018 10:12, edited 1 time in total.
Re: Code example for Dir not working
The 1st wiki code example also works on my PC (Win10)!
Can you provide precision on your dysfunction (compiler error?, execution error?, ...)
Passing arguments to procedures:
- Parentheses surrounding the argument list (if any) are required only for function calls in expressions.
- If there is no argument to pass, the parentheses are always optional, but it is a common convention to place empty parentheses '()' after the function name, to signify a procedure call.
Can you provide precision on your dysfunction (compiler error?, execution error?, ...)
Passing arguments to procedures:
- Parentheses surrounding the argument list (if any) are required only for function calls in expressions.
- If there is no argument to pass, the parentheses are always optional, but it is a common convention to place empty parentheses '()' after the function name, to signify a procedure call.
Last edited by fxm on Mar 17, 2018 9:46, edited 5 times in total.
Re: Code example for Dir not working
Generally there is no need for parenthesis in calling a sub with parameters.
Similar for a function, unless you want access to the function return value.
Of course it would help greatly if you can search for sub in the online help and see the square(optional) brackets.
But you cannot (only three characters in sub)
But it seems only the .chm help file is place for help on sub.
(FreebASIC 1.05 here)
What version do you use?
EDIT
Sorry, didn't see intermediate post by fxm
EDIT
Or SARG.
Similar for a function, unless you want access to the function return value.
Code: Select all
sub T1(x as long,y as long)
print x;",";y
end sub
function T2(x as long,y as long) as string
return str(x) +", "+str(y)
end function
T1 5,6
T2 5,6
print T2(5,6)
sleep
But you cannot (only three characters in sub)
But it seems only the .chm help file is place for help on sub.
(FreebASIC 1.05 here)
What version do you use?
EDIT
Sorry, didn't see intermediate post by fxm
EDIT
Or SARG.
Re: Code example for Dir not working
guys, thanks for the replies.
i am not exactly sure what the error i got first time was when i tried the Dir example, as i did not grab a screenshot at the time - i might have to start fresh and see if i can replicate it. i reviewed some other code and added the parenthesis, but was consistently getting "compiler error, linking failed: 'R:\data_papps\FreeBASIC\bin\win64\ld.exe' terminated with exit code 1" - i have a habit of working on code, and compile after save changes without closing the src file - i have been doing that for a long time with other fb code i am writing with notepad++ with no problems, so i closed notepad++ , recompiled and all good. embarassing!
Edit: interesting factoid - if i wait 10 seconds before compiling i usually compile ok. sometimes though i have to compile a second time, so I am assuming notepad++ is holding a file handle open for a while longer than i would like!
the fact that parenthesis around the parameter_list are not essential is not clear in the documentation in the KeyPgSub or KeyPgFunction pages and if parenthesis were used in all the Dir command, Sub and Function code examples i would not have made the original post thinking there was an error - many thanks for the clarification!
feedback is always valuable though and it made me go back to the issue and rethink. doing a quick scan of various pages in the manual i can see room for improvement - maybe clarifying parts of the fb manual documentation for new/inexperienced users that help speed comprehension/understanding at first read is an area that i can contribute to.
i am not exactly sure what the error i got first time was when i tried the Dir example, as i did not grab a screenshot at the time - i might have to start fresh and see if i can replicate it. i reviewed some other code and added the parenthesis, but was consistently getting "compiler error, linking failed: 'R:\data_papps\FreeBASIC\bin\win64\ld.exe' terminated with exit code 1" - i have a habit of working on code, and compile after save changes without closing the src file - i have been doing that for a long time with other fb code i am writing with notepad++ with no problems, so i closed notepad++ , recompiled and all good. embarassing!
Edit: interesting factoid - if i wait 10 seconds before compiling i usually compile ok. sometimes though i have to compile a second time, so I am assuming notepad++ is holding a file handle open for a while longer than i would like!
the fact that parenthesis around the parameter_list are not essential is not clear in the documentation in the KeyPgSub or KeyPgFunction pages and if parenthesis were used in all the Dir command, Sub and Function code examples i would not have made the original post thinking there was an error - many thanks for the clarification!
feedback is always valuable though and it made me go back to the issue and rethink. doing a quick scan of various pages in the manual i can see room for improvement - maybe clarifying parts of the fb manual documentation for new/inexperienced users that help speed comprehension/understanding at first read is an area that i can contribute to.
Last edited by andykmv on Mar 28, 2018 20:59, edited 1 time in total.
Re: Code example for Dir not working
Documentation updated:
KeyPgSub → fxm [Added a rule on using parentheses when calling a Sub]
KeyPgFunction → fxm [Added a rule on using parentheses when calling a Function]
KeyPgSub → fxm [Added a rule on using parentheses when calling a Sub]
KeyPgFunction → fxm [Added a rule on using parentheses when calling a Function]
Re: Code example for Dir not working
@fxm - thats great. I am also wondering if you can add a clarification to the Dir() Description. It wasnt clear to me that Dir needs to be invoked multiple times to get a list of files in a dir (i now get it). Could some additional explanatory text be added at the start of the Description for Dir() to make that clear ? for example:
the current text reads:
Description:
If item_spec contains an absolute path, then the first procedure searches the filesystem for an item ....
.(and further down it reads)...If found, subsequent calls with item_spec omitted, or set to an empty string, will return the next item matching the name item_spec....
suggested addition:
Description:
To obtain a list of files in a directory, Dir() needs to be invoked multiple times returning one result per invokation until 0 is returned. If item_spec contains an absolute path, then the first procedure searches the filesystem for an item ....
.(and further down it reads)...If found, subsequent calls with item_spec omitted, or set to an empty string, will return the next item matching the name item_spec....
as you then read the Description text, the first paragraph is now clear at the outset about what you get for the first invocation, and then what you get for subsequent invocations with item_spec omitted etc.
the current text reads:
Description:
If item_spec contains an absolute path, then the first procedure searches the filesystem for an item ....
.(and further down it reads)...If found, subsequent calls with item_spec omitted, or set to an empty string, will return the next item matching the name item_spec....
suggested addition:
Description:
To obtain a list of files in a directory, Dir() needs to be invoked multiple times returning one result per invokation until 0 is returned. If item_spec contains an absolute path, then the first procedure searches the filesystem for an item ....
.(and further down it reads)...If found, subsequent calls with item_spec omitted, or set to an empty string, will return the next item matching the name item_spec....
as you then read the Description text, the first paragraph is now clear at the outset about what you get for the first invocation, and then what you get for subsequent invocations with item_spec omitted etc.
Re: Code example for Dir not working
Done:
KeyPgDir → fxm [Added clarification to the DIR description versus successive calls]
KeyPgDir → fxm [Added clarification to the DIR description versus successive calls]
Re: Code example for Dir not working
How about adding a comment in the first example?
From:
To:
From:
Code: Select all
Sub list_files (ByRef filespec As String, ByVal attrib As Integer)
Dim As String filename = Dir(filespec, attrib) ' Start a file search with the specified filespec/attrib *AND* get the first filename.
Do While Len(filename) > 0 ' If len(filename) is 0, exit the loop: no more filenames are left to be read.
Print filename
filename = Dir()
Loop
End Sub
Code: Select all
Sub list_files (ByRef filespec As String, ByVal attrib As Integer)
Dim As String filename = Dir(filespec, attrib) ' Start a file search with the specified filespec/attrib *AND* get the first filename.
Do While Len(filename) > 0 ' If len(filename) is 0, exit the loop: no more filenames are left to be read.
Print filename
filename = Dir() ' Search for (and get) the next item matching the initially specified filespec/attrib
Loop
End Sub
Re: Code example for Dir not working
OK.
Done.
Done.