Hello

I have this problem in pascal:

I want to include a variable in file name while assigning a text file to variable of textfile.

Example:

I have a text file called 'numoffiles.txt' with one integer number in it
I read it and set variable a to that number in file (-read(f,a)--)
Then I close 'numoffiles.txt'
I have a files file_1, file_2, file_3 and so on in same folder
I want to open in pascal the number of files defined by number in file 'numoffiles.txt', which is now the value of variable a
So when I type in 'numoffiles.txt' '2', I want to open in pascal just file_1 and file_2

I was thinking about something like this:

for i:= 1 to a do begin

assign ([B]f,'file_',i,'.txt'[/B]); ([U]include variable into file name[/U])
reset (f);
read (something unimportant here);
close (f);

end;

But it doesnt work
So just want to know if there is a way how to define the number of files that are going to be read

I dont want to read every file separately (but of course - if its only way i'll have to)

Hope you at least understand definition of my problem
Thanks for answers

for i := 1 to a do
   begin
     name := Format('file_%d.txt',[i]);
     if FileExists(name) then
     begin
       assign (f,name); (include variable into file name)
       reset (f);
       read (something unimportant here);
       close (f);
     end;
   end;

Sorry, it doesn't work. Im using freepascal, could be the problem in it?

It says identifier not found ''name'', ''format'', ''fileexists''.
I supposed they are functions, but it seems my pasacl doesnt know them.
It's possible I wrote it wrong, but I wrote it exactly as its writen here.

Have any advice?
Thanks

It Delphi function. Tests if a specified file exists.
File: SysUtils
Description:
FileExists returns true if the file specified by FileName exists. If the file does not exist, FileExists returns false.

errCount := 0;
   for i := 1 to a do
   try
     name := Format('file_%d.txt',[i]);
     assign (f,name); (include variable into file name)
     reset (f);
     read (something unimportant here);
     close (f);
   except
     inc(errCount);
   end;

   id errCount <> 0 then
      ........

I understand what does fileexists mean, but my freepascal doesnt. It writes: identifier not found.

So, any ideas?
Like I said, my pascal doesnt know theese commands: fileexists, name, format.

Hello everyone,

Reading this post I note two areas where I can add my own opinion and (if possible) my programmer's experience.

First, when using the Assign(...) and Reset(....) procedures, one should prevent program crashes when the expected file is not there or any I/O error occurs.
In TurboPascal and Delphi this is achieved using the compiler directive {$I ±} like this:

AssignFile( ff, my_file_name ); // AssignFile) is the Delphi equiv of Assign(
{$I-} Reset(ff), {$I+} // {$I-} turns off the I/O error reporting
ErrorCode := IOResult; // IOResult is a function reporting the status of the I/O
if ErrorCode > 0 then ... report an error here according

Similarily one should use this compiler directive with the Close() procedure (in case the file haden't been opened because of an error, this would prevent a second error)


Second, if FileExists( does not exist in Free pascal, find if a similar compiler directive to $I in FreePascal and use the following small function....

function FileExists( filename: String): boolean;
var ff : TextFile; // or : Text
begin
Result := False; // Early leaves are concidered as file doesn't exist
AssignFile( ff, filename );
{$I-} Reset(ff); {$I+}
if IOResult > 0 then Exit; // Some I/O error occured - leave function
// If we get here, the file opened successfully so we have to close-it
Result := True;
{$I-} CloseFile(ff); {$I+} // or Close(ff)
end; // FileExists

Marcel

Sorry, but this still doesnt solve my problem. I dont care about any 'fileexists' function, because I know that that file exists.
My problem is that i cant include a value of variable into file name.

What is the problem? Do not use such functions. The file name can be formed so:

for i := 1 to a do
     name := Format('file_%d.txt',[i]);
.........
  for i := 1 to a do
     name := 'file_' + IntToStr(i) + '.txt';

 /// add current path

name := ExtractFilePath(ParamStr(0)) + name;

Ahh...
The ''Format'' function doesnt as well, I wrote it above, so still no success.
And ''name''function doesnt work too.

may be this will help ..

uses sysutils ;
var
	a,i : integer ; b,r : text ; fname,g:string ;
begin
	assign (b,'numoffiles.txt');
	reset(b);
	read(b,a);
	for i := 1 to a do 
		begin
			g := inttostr(i);
			fname := 'file_'+ g ;
			assign(r,fname);
			reset (r);
			{blah blah blah ;}
			close(r);
		end;
	close(b);
end.

Edited 6 Years Ago by belthazor: n/a

This article has been dead for over six months. Start a new discussion instead.