Hello!
I'm trying to implement a procedure and a function involving findfiles, which, afaik, can do path search.
One thing at a time. First is the function. A sample would be
--snip-- program FindFilesTest;
uses GPC, FileUtils;
type alphafile=text; var nameoffile: string; filename: alphafile; const mydir='/usr/local/';
function aopenin(var a:alphafile):boolean; begin if ioresult=0 then assign(a,trim(nameoffile)); findfiles(mydir,a,false,reset,nil); aopenin:=ioresult=0; end;
begin WriteLn ('Enter file name to type: '); ReadLn (nameoffile); if aopenin(alphafile) then WriteLn(nameoffile) else Writeln('sorry, search failed'); end.
--snip--
The compiler bites me back saying:
syntax error before reset extra comma
Beats my why.
Thanks in advance,
How about
1) A neatly formatted program, so those you are asking for help can read it easily. I'll rewrite it below for you.
2) A clear statement of what computer and O/S you are working on.
If it is a DOS/Windows machine, then
mydir='/usr/local/';
should be
MyDir = '\usr\local';
3) I am unfamiliar with the procedure
findfiles
Why don't you document it clearly.
4) Do you get a better result if you omit the middle man
alphafile, and use text directly?
5) I believe a statement
A := B = C;
is always an error. See my note below.
HF
Luis Rivera wrote:
Hello!
I'm trying to implement a procedure and a function involving findfiles, which, afaik, can do path search.
One thing at a time. First is the function. A sample would be
--snip-- program FindFilesTest;
uses GPC, FileUtils;
type alphafile=text; var nameoffile: string; filename: alphafile; const mydir='/usr/local/';
function aopenin(var a:alphafile):boolean; begin if ioresult=0 then assign(a,trim(nameoffile)); findfiles(mydir,a,false,reset,nil); aopenin:=ioresult=0; end;
begin WriteLn ('Enter file name to type: '); ReadLn (nameoffile); if aopenin(alphafile) then WriteLn(nameoffile) else Writeln('sorry, search failed'); end.
--snip--
The compiler bites me back saying:
syntax error before reset extra comma
Beats my why.
Thanks in advance,
========================================
program FindFilesTest;
uses GPC, FileUtils;
type AlphaFile = Text;
var NameOfFile: string; FileName: AlphaFile;
const MyDir='\usr\local';
function AOpenIn(var A: AlphaFile): Boolean;
begin if IORresult = 0 then Assign(A, Trim(NameOfFile)); FindFiles(MyDir, A, False, Reset, nil); AOpenIn := (IOResult = 0); { NOTE THE ADDED () } end;
begin WriteLn('Enter file name to type: '); ReadLn(NameOfFile); if AOpenIn(AlphaFile) then WriteLn(NameOfFile) else Writeln('Sorry, search failed.'); end.
From the reference:
type TStringProc = procedure (const s: String);
procedure FindFiles (const Directory, Mask: String; MainDirFirst: Boolean; FileAction, DirAction: TStringProc); attribute (iocritical);
How you call it: findfiles(mydir,a,false,reset,nil);
mydir is of type string => matches Directory type a is of type text => doesn't match Mask type false is of type boolean => matches MainDirFirst type reset is a procedure to open file for reading, it's of type procedure (f: text) for text files => doesn't match FileAction type TStringProc (see above) nil is a pointer pointing to address 0, which is a reserved location for unallocated vars => doesn't match DirAction type TStringProc (see above)
Can you see where the mistakes are now?
Luis Rivera a écrit :
program FindFilesTest;
uses GPC, FileUtils;
type alphafile=text; var nameoffile: string; filename: alphafile; const mydir='/usr/local/';
function aopenin(var a:alphafile):boolean; begin if ioresult=0 then assign(a,trim(nameoffile)); findfiles(mydir,a,false,reset,nil); aopenin:=ioresult=0; end;
begin WriteLn ('Enter file name to type: '); ReadLn (nameoffile); if aopenin(alphafile) then
should be aopenin(filename)
WriteLn(nameoffile) else Writeln('sorry, search failed'); end.
Declaring the text file as filename is very confusing: this is certainly the source of your errors: you confuse a file and its name.
The errors are seen by looking to the declarations
type TStringProc = procedure (const s: String);
procedure FindFiles (const Directory, Mask: String; MainDirFirst: Boolean; FileAction, DirAction: TStringProc); attribute (iocritical);
in the call of findfiles there are two errors:
1) a is a file, not a string (Mask)
2) reset has for parameter a file, not a string.
You might want to declare a FileAction procedure with a string parameter (nameoffile): probably you would include in it the assign which is before findfiles in your program and the reset
Notice that if there are multiple files found, you will have an error because you then try to reopen a file (a) which has not been closed: findfiles apply FileAction on all files which match Mask. You can close the file inside FileAction, but beware not to confuse nameoffile as a parameter of FileAction and as a global variable: if you give two different names and assign the global name to the parameter inside FileAction you can recover at the end the last nameoffile found.
Maurice
PS:
A := B = C; is correct in Pascal, weird but correct.
Maurice Lombardi wrote:
PS:
A := B = C; is correct in Pascal, weird but correct.
Indeed. This is not even a question of operator precedence, since in contrast to C, the assignment in Pascal (":=") is not an operator. In fact, the syntax for an assignment statement is:
assignment-statement = ( variable-access | function-identifier ) `:=' expression .
And since neither variable-access nor function-identifier or expression can contain a ":=" token, there cannot be any ambiguity without parentheses.
It may look strange if you mentally consider ":=" as a kind of "=", since an expression like "a = b = c" is indeed invalid in Pascal. That's why some might prefer to put in the parentheses. I myself don't, just like I don't add unnecessary parentheses, e.g., in "if" statements such as "if a = b then ..." (in contrast to "if (a = b) and (c = d) then ..." where they're required).
@Luis Rivera: If the replies so far didn't help you, you might describe what you actually want to achieve, so we can help you better. If you're just looking for any example using FindFiles, you can look at findfilesdemo.pas under demos.
Frank
On 23 May 2010 19:22, Frank Heckenbach ih8mj@fjf.gnu.de wrote:
@Luis Rivera: If the replies so far didn't help you, you might describe what you actually want to achieve, so we can help you better. If you're just looking for any example using FindFiles, you can look at findfilesdemo.pas under demos.
Basically, I'm trying to find a file whose name is provided by the user in an interactive console session which might be found somewhere in a directory other than the current one, with path search, and reset the contents of the file ready for further processing.
I hope I've explained it right...
Thanks in advance,
On 24 May 2010 12:37, Luis Rivera jlrn77@gmail.com wrote:
On 23 May 2010 19:22, Frank Heckenbach ih8mj@fjf.gnu.de wrote:
@Luis Rivera: If the replies so far didn't help you, you might describe what you actually want to achieve, so we can help you better. If you're just looking for any example using FindFiles, you can look at findfilesdemo.pas under demos.
Basically, I'm trying to find a file whose name is provided by the user in an interactive console session which might be found somewhere in a directory other than the current one, with path search, and reset the contents of the file ready for further processing.
I failed to explain that this function is meant to simply check if the file exists; and if so, reset it and pass it over for further processing in other procedures.
Cheers,