6
votes

The Question: I recently acquired a 1989 IBM PS2 and I am trying move large files from my newer UNIX-based machine to this IBM via floppy. I have a bash script that splits my files into ~2MB chunks, now I am trying to write a pascal program to reconstruct these files after they have been transferred.

I am unable to find the correct read/write to file methods on this computer. I have tried various pascal tutorial sites, but they are all for newer versions (the site I followed with File Handling In Pascal). I am able to create an empty file (as described below), but I am unable to write to it. Does anyone know the correct pascal read and write methods for this type of computer?

I know this is an obscure question, so thank you in advance for any help you can give me!

The Details:

The current test code that creates a file correctly is this:

program testingFiles;
uses Crt, Win;

const FILE_NAME = 'testFile.txt';
var outFile : File;

begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);
rewrite(outFile);    

end.

This is some test code that does not work, the method's append() and close() could not be found:

program testingFiles;
uses Crt, Win;

const FILE_NAME = 'testFile.txt';
var outFile : File;

begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);

append(outFile);
writeln('this should be in the file');
close(outFile);

end.

This is an alternative that also did not work, the writeln() method only ever prints to the terminal. But otherwise this does compile.

program testingFiles;
uses Crt, Win;

const FILE_NAME = 'testFile.txt';
var outFile : File;

begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);

rewrite(outFile);
writeln('this should be in the file');
close(outFile);

end.

The system: As was previously mentioned, this is a 1989 IBM PS2.

  • It has Windows 3.0 installed and can also run DOS and MS-DOS terminals.
  • It has Microsoft SMARTDrive Disk Cache version 3.06
  • It has Turbo Pascal 5.5 installed and I am using turbo as my command line pascal editor. (the readme was last updated in 1989)
  • It has Turbo debugger 1.5 installed.

Again, I know this is an obscure question, so thank you in advance for any help you can give me!

2
The question's not that obscure. Basic Pascal file I/O hasn't changed in a very long time. Are you just doing this as an exercise? You know that DOS COPY can concatenate files, even binary ones using the /B option. E.g., COPY File1/B + File2/B File3. But if you want to do this in Pascal, did you look up file handling in Pascal?lurker
@lurker Thanks for the COPY knowledge, I actually did not know that and I'll give it a try (I'm a linux guy)! But I have read that page, it is the first link in the question. I'll change the link title so that it is more apparent, thanks.Caleb Adams
I don't think you need append to concatenate files if you leave the output file open during the whole process. You would only need rewrite to open the new output file initially for writing. In your second example that compiles, why aren't you using writeln(outFile, ...) as described in the page you linked to? That should work in even the oldest Pascal compilers (it goes back to Niklaus Wirth 1974). How is it that your second example compiles with close but your first one can't find close?lurker
WHy are you importing unit win? That can only cause trouble, since I assume you aren't using some win3.xMarco van de Voort
I did a little research and the append function should be available at least as far back as Turbo Pascal 3.0. So when you say append and close could not be found, there's something else wrong. The compiler supports them.lurker

2 Answers

2
votes

My Pascal memory is VERY rusty... but as other have pointed out, here is what you should consider:

program testingFiles;
uses Crt, System;
//No need of importin Win Win is for Windows enviorment, however I'm not sure if you need to use System, Sysutils or was there a Dos class???

const FILE_NAME = 'testFile.txt';
var outFile,inFile : File;

begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);
rewrite(outFile);

//Now Open the first chunk of the file you want to concatenate

AssignFile(inFile, "fisrt_chunk.dat");
reset(inFile);

while not eof(inFile) do
 begin
   readln(inFile, s);
   writeln(outFile,s);
 end;
close(inFile);

end.

I don't have Turbo/Borland Pascal installed any longer so I couldn't compile it myself, no promise that it will work it is more like an idea:

  • Key thing to remember, readln and writeln will ALWAYS add a return at the end of the string/line, read and write on the other hand will leave the cursor wherever it is without jumping to a new line.
1
votes

Here's some old Delphi code that should be at least close to syntax-compatible that will give you the gist of copying a file (with limited error checking and resource handling in case of error - I'll leave that as an exercise for you). It works to copy both binary and text content.

program Project2;

uses
  SysUtils;

var
  NumRead, NumWritten: LongInt;
  pBuff : PChar;
  SrcFile, DstFile: File;
const
  BuffSize = 2048;  // 2K buffer. Remember not much RAM available    

  InFileName = 'somefile.txt';
  OutFileName = 'newfile.txt';
begin
  NumRead := 0;
  NumWritten := 0;

  AssignFile(SrcFile, InFileName);
  AssignFile(DstFile, OutFileName);

  // Allocate memory for the buffer
  GetMem(pBuff, BuffSize);

  FileMode := 0;            // Make input read-only
  Reset( SrcFile, 1 );

  FileMode := 2;            // Output file read/write
  Rewrite( DstFile, 1 );

  repeat
    // Read a buffer full from input
    BlockRead(SrcFile, pBuff^, BuffSize, NumRead);

    // Write it to output
    BlockWrite(DstFile, pBuff^, NumRead, NumWritten);
  until (NumRead = 0) or (NumWritten <> NumRead);

  // Cleanup stuff. Should be protected in a try..finally,
  // of course.
  CloseFile(SrcFile);
  CloseFile(DstFile);
  FreeMem(pBuff);
end.

The above code compiles under Delphi 2007 currently (the oldest version I have installed). (See the note below.)

As a side note, this was from an archived version of some code I had that compiled both for 16-bit Delphi 1 and was extended to also compile under 32-bit Delphi 2 back in the mid-to-late 90s. It's still hanging around in my source repositories in an old tagged branch. I think I need to do some pruning. :-) I cleaned it up to remove some other functionality and removed a lot of {$IFDEF WIN32} ... {$ELSE} ... {$ENDIF} stuff before posting.)