?
Solved

Automate pascal program command line input

Posted on 2009-04-07
8
Medium Priority
?
2,082 Views
Last Modified: 2012-05-06
I am looking to automate a pascal program to accept the readlnI() input automatically either via a parameter file or as parameters when the program is executed on the command line.

I am not really familiar with pascal and I am wondering how best to modify this source code such that the program will accept it's 'input' and 'output' filenames as parameters or if no parameters are supplied, prompt for the user to input them as the program is executing.
Program SPMake;
var
  f : text;
  w : text;
  In_file : String[12];
  In_pref : String[8];
  out_file : String[12];
  lines   : longint;
  suffix  : String[3];
  Alldone : Boolean;
  file_no : integer;
  c : char;
  asc : longint;
 
begin
  Write('Name of SPOOL files -> ');
  Readln(In_file);
  if (length(In_file)=0) or (pos('.',In_file)>0) then
  begin
    writeln('Invalid SPOOL file name.');
    halt;
  end;
 
  Write('File to be created -> ');
  Readln(Out_file);
 
  if (length(Out_file)=0) or (pos('.',Out_file)>9)
  or ((pos('.',In_file)=0) and (length(In_file)>8)) 
  or (length(Out_file)>12)   then
  begin
    writeln('Invalid output file name.');
    halt;
  end;
  In_Pref:=In_file;
 
  Assign(w, out_file);
  Rewrite(w);
    
 
 
  Alldone := False;
 
  file_no:=0;
  while not alldone do 
  Begin
    file_no:=file_no+1;
    str(file_no,suffix);
    while length(suffix)<3 do suffix:='0'+suffix;
 
    In_file:=In_pref+'.'+suffix;  
    Assign(f, In_file);
    {$I-}
    Reset(f);
    {$I+}
    If IOResult<>0 then
    begin
      Alldone:=True;
      if suffix='001' then 
      begin
        write('File ',in_file,' not found.');
        halt;
      end;
    end else
    begin  
      lines:=0;
      writeln('Reading ', In_file, ' into ',out_file);
      while not eof(f)  do
      begin
        read(f,c);
        asc:=ord(c);
        if (asc>9) and (asc<>29) then write(w,c);
 
        {if (asc>= 13) and (asc <= 126) then write(w,c);}
      end;
      close(f);
      erase(f);
    end;
  end;    
  Close(w);
  Writeln('Files combined.');
end.

Open in new window

foxbase-screenshot.JPG
0
Comment
Question by:james_j_hedges
  • 4
  • 3
8 Comments
 
LVL 29

Assisted Solution

by:Bill Bach
Bill Bach earned 400 total points
ID: 24088694
In the C language, the main() function accepts two arguments, known commonly as argc and argv, which contains the count and the values or each command-line argument.  I would expect that your PASCAL compiler will have a similar feature.  A quick search seems to point to using an array called ParamStr[n] when n=0 is the program name, n=1 is the first parameter, and so on.  However, checking your specific compiler manual is the best way to figure this out.  Here's some links to get you started, though:
    http://www.hkbu.edu.hk/~bba_ism/ISM2110/pas074.htm
    http://www.mirrorservice.org/sites/www.gnu-pascal.de/gpc/Accessing-Command-Line-Arguments.html
0
 
LVL 50

Expert Comment

by:dbrunton
ID: 24095242
This looks like Turbo Pascal.

In which case the code you want is the ParamCount and ParamStr functions.

if Paramcount < 1 then
writeln('No parameters on command line')
else
writeln(Paramcount, ' paramete(s)');


for i := 1 to Paramcount do
writeln(ParamStr(i));

Paramstr(index) is an array of the command line parameters.  Blanks and tabs separate the parameters on the command line.
0
 

Author Comment

by:james_j_hedges
ID: 24095282
Thanks for the advice. How did you determine whether the code is 'Pascal' or 'Turbo Pascal'? I'm assuming that I will need to use a different compiler depending on the language?

Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 50

Accepted Solution

by:
dbrunton earned 1600 total points
ID: 24095295
This is the type of code you are wanting.

GetBothFileNames is a procedure that does what you are doing now.  Your present file name getting code goes into this procedure.

GetOutFileName only gets the file name for the out file.  It is a subset of your present code.

These two lines assign the command line parameters to the in and out files.

InFile := ParamStr(1);
OutFile := ParamStr(2);


if ParamCount = 0 then
	GetBothFileNames
else  if ParamCount = 1 then
begin
	InFile := ParamStr(1);
	GetOutFileName
end
else
begin
	InFile := ParamStr(1);
	OutFile := ParamStr(2);
end;

Open in new window

0
 
LVL 50

Assisted Solution

by:dbrunton
dbrunton earned 1600 total points
ID: 24095375
Actually it could be standard Pascal.

I assumed the IOResult code was a Turbo Pascal function.  It's not.  It's in a couple of other Pascals as well.

This compiler directive

{$I+}

is Turbo Pascal plus it's used in other Pascal compilers such as Free Pascal and GNU Pascal.

The code for your file names looks like it's designed for DOS from the way the filenames are being created and checked.  The most common DOS Pascal compiler was the Turbo Pascal range.

But which compiler are you using for your code?  Several of the Turbo Pascal compilers are free and readily available.
0
 

Author Comment

by:james_j_hedges
ID: 24095483
I'm not really sure which compiler is being used. I would imagine maybe Turbo Pascal. Do have any suggestions as to which specific compiler to use?

Thanks
0
 
LVL 50

Assisted Solution

by:dbrunton
dbrunton earned 1600 total points
ID: 24095552
Turbo Pascal 5.5

http://edn.embarcadero.com/article/20803

You'll need to create a free account to get to the download site.  There's a few tricks involved to get it running on Windows 2000 and XP but all of those are fixable/solvable.

Under Windows 9x or DOS there are no problems.  You may strike problems with fast CPUs but those are fixable as well.


Free Pascal would probably do the job as well.  http://www.freepascal.org/

Shouldn't be any problems getting it running under Win 2000 and XP.
0
 

Author Comment

by:james_j_hedges
ID: 24097084
I've managed to update the pascal program to successfully command line parameters.

Thanks for your help!
Program SPMake (Output);
var
	arguments : integer;
  f : text;
  w : text;
  In_file : String[12];
  In_pref : String[8];
  Out_file : String[12];
  lines   : longint;
  suffix  : String[3];
  Alldone : Boolean;
  file_no : integer;
  c : char;
  asc : longint;
 
begin
 
	if ParamCount = 0 then
		begin
		Writeln('Please enter Input SPOOL files and Output SPOOL file names as parameters');
		end
	else if ParamCount = 1 then
					begin
			 		Writeln('Please enter both Input SPOOL files and Output SPOOL file names as parameters');
			 		end
			 else 
			 begin
  				In_file := ParamStr(1);
  				Out_file := ParamStr(2);
	end;
	
  if (length(In_file)=0) or (pos('.',In_file)>0) then
  begin
    writeln('Invalid SPOOL file name.');
    halt;
  end;

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question