6

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!

Caleb Adams
  • 4,445
  • 1
  • 26
  • 26
  • 3
    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](http://wiki.freepascal.org/File_Handling_In_Pascal)? – lurker Jan 29 '16 at 04:20
  • @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 Jan 29 '16 at 04:24
  • 1
    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 Jan 29 '16 at 04:45
  • 1
    WHy are you importing unit win? That can only cause trouble, since I assume you aren't using some win3.x – Marco van de Voort Jan 29 '16 at 08:59
  • 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 Jan 29 '16 at 17:12
  • The `writeln` in your second example will work if you write to file instead with `WriteLn(outfile, 'This should be in the file');`. However, `writeln` is for writing text output only; it appends a CR/LF to the end of the output. – Ken White Jan 29 '16 at 23:26
  • There should be complete filesplitting examples in SWAG/SIMTEL sources. – Marco van de Voort Feb 18 '16 at 10:59
  • @lurker it's `copy /b file1+file2+...` – phuclv Mar 02 '16 at 14:00
  • @LưuVĩnhPhúc yes, that's more concise. Either format works actually. – lurker Mar 02 '16 at 14:52

2 Answers2

2

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.
Emil Borconi
  • 3,326
  • 2
  • 24
  • 40
  • (1) assignfile is delphi, not TP. (2) use "text" as filetype for text I/O. (3) closing outfile is nice too. (4) pascal is declare before use, so it is nice to declare "s". Anyway, OP probably wants a blockread/write solution – Marco van de Voort Mar 02 '16 at 16:27
1

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.)

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • closefile+Assignfile is delphi only, but replacing it with assign and close should work. the last parameters of blockwrite and read will be integer or word (which are 16-bit), and not 32-bit longint, since you can't work with blocks >16-bit in TP using normal means (getmem etc)Using a static array for buffer would make the code simpler. – Marco van de Voort Feb 18 '16 at 10:58
  • and iirc freemem needs size in TP. – Marco van de Voort Mar 02 '16 at 16:28
  • @Marco: Good points. Except I did say that I was posting *Delphi* code that would get the poster started. I didn't post TP code, because I don't have a TP compiler to test it on first. :-) – Ken White Mar 02 '16 at 18:43
  • So now you can correct it so that it becomes TP code :-) – Marco van de Voort Mar 02 '16 at 19:55
  • When I declare the pBuff I get the error `PChar Error 3: Unknown identifier` :'( – Caleb Adams Mar 03 '16 at 01:57
  • @Caleb: What compiler and version are you using? You've not given any information to go on. – Ken White Mar 03 '16 at 02:23