Reading and writing to a txt file in Free Pascal

M S
M S used Ask the Experts™
on
I am going to be doing some simple text processing with Free Pascal.  I will have to read in a text file, chop the text up a bit, and then write to a new file.  I looked for a sample program online and it talked about a dat file.  I don't remember how this relates to a txt file.  My input file is a txt file.

Years ago I was working in a unix environment, and I vaguely remember doing things with < or > to tell the operating system what input file to use and what output file to use.  How do you tell Free Pascal what your input and output files are?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer
Fellow 2017
Most Valuable Expert 2018
Commented:
Here's a sample program that should help you:

program ReadWriteText;
var
  namein,nameout: string[255];
  line: ansistring;
  textin,textout: text;
begin
  write('Enter name of input file: ');
  readln(namein);
  write('Enter name of output file: ');
  readln(nameout);
  assign(textin,namein);
  reset(textin);
  assign(textout,nameout);
  rewrite(textout);
  while not eof(textin) do begin
    readln(textin,line);
    writeln(textout,line);
  end;
  close(textout);
end.

Open in new window

Tested in Free Pascal 3.0.4 — worked perfectly. Regards, Joe
M S

Author

Commented:
Thanks.  If I remember right, this would limit the input string to 255 characters?  That would be a problem.  It's going to be a LOT more than that.

Not sure if this will help: the input file is structured into a bunch of quasi-records, each beginning with a blank (empty) line.  (In Word it looks like a double carriage return instead of a single carriage return.)
M S

Author

Commented:
I found something: https://stackoverflow.com/a/20220110/445686

{$H+}

var s: String;

begin
    s := StringOfChar('X', 1000);
    writeln(s);
end.

I'm going to give that a try.
M S

Author

Commented:
Oh, jeez.  I just understood the 255.  That's the character limit for the file name.  Sorry!  Okay, I'm going to try your code with the big text file and if that doesn't work I'll try it with a smaller version.
M S

Author

Commented:
The following is working:

program AppendDemo;
     var
       Sample: Text;
     begin
       Assign (Sample, 'sample.txt');
       Rewrite (Sample);
       WriteLn (Sample, 'Hello, World!');  { `sample.txt' now has one line }
       Close (Sample);
     
       { ... }
     
       Append (Sample);
       WriteLn (Sample, 'Hi again!');  { `sample.txt' now has two lines }
       Close (Sample)
     end.

Open in new window


Now to get the read working.  I notice this code is very similar to what you gave me, Joe, I shouldn't have monkeyed with it!
M S

Author

Commented:
Success.

program textProcess5;
var
  namein,nameout: string[255];
  line: ansistring;
  textin,textout: text;
begin
  namein := 'input_file.txt';
  nameout := 'output_file.txt';
  assign(textin,namein);
  reset(textin);
  assign(textout,nameout);
  rewrite(textout);
  while not eof(textin) do begin
    readln(textin,line);
    writeln(textout,line);
  end;
  close(textout);
end.

Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
I'm glad to see that you answered all your own questions, but a few comments, anyway:

> Oh, jeez. I just understood the 255. That's the character limit for the file name. Sorry!

Yes, I made the variables for the file names a ShortString and set it to the max short string length, which is 255. I made the variable for the text line in the file an AnsiString, which has a variable length that is limited only by the host platform.

> Success

I'm sure you realize that the only difference between your "Success" code and the code that I posted is that you hard-coded the input and output file names. In general, it is better programming practice not to hard-code, although it's certainly an acceptable practice in test programs. In this case, I wanted to give you the ability to enter the input and output file names rather than hard-coding them in assignment statements, as your "Success" code does. Regards, Joe

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial