Can anyone tell me the format for reading information into a program and if I want to save information out of the prgram into a txt file.

I understand it to be

Assign (file name, path)
reset (filename)
close (file name)


and to write information out

assign (filename, path);
rewrite(filename, with all the information)
close (filename)


Which brings up another question: Is it possible to read the txt file line by line and then save it out to another txt file line by line or must it be done as a entir file?

Dani AI

Generated

Useful clarifications and a safer pattern to follow. ’s question and ’s reply show the right approach; two practical improvements make that example reliable in real programs: read each line into a string variable before writing it out, and guarantee files are closed even if an error occurs. In Delphi/Free Pascal it’s common to use AssignFile/CloseFile to avoid collisions with object Assign; older Turbo Pascal uses Assign/Close.

A concise, robust line-by-line pattern (read into a variable and ensure closure):

var
  InF, OutF: Text;
  Line: string;
begin
  AssignFile(InF, 'in.txt');
  AssignFile(OutF, 'out.txt');
  Reset(InF);
  Rewrite(OutF);
  try
    while not EOF(InF) do
    begin
      ReadLn(InF, Line);     // capture the input line
      // optional processing of Line goes here
      WriteLn(OutF, Line);   // write the processed line out
    end;
  finally
    CloseFile(InF);
    CloseFile(OutF);
  end;
end;

A few extra tips not shown above: use Append if you want to add to an existing file instead of erasing it with Rewrite. For small files, TStringList.LoadFromFile / SaveToFile is often simpler and lets you manipulate lines by index. For large files or binary copies, prefer TFileStream/stream-based copying to avoid high memory use. Watch encoding and line-ending differences (UTF-8 vs ANSI, LF vs CRLF) when moving files between systems—text routines don’t convert encodings automatically. Finally, handle errors appropriately: Delphi raises EInOutError for I/O exceptions; in older compilers check IOResult. These practices make line-by-line file copying predictable and safe.

File I/O line-by-line (for text files) is fairly simple.

Assign associates an external filename with a file variable. This is the first step in file I/O. After the Assign, all references to the file are made through the file variable.

Reset opens a file for reading, preserving the contents and placing the file position at the beginning.

Rewrite opens a file for writing, destroying the contents and placing the file position at the beginning.

For Reset and Rewrite: If the file is an untyped file (i.e. F : file ), an optional record size parameter can be passed to the call (i.e. Reset( F, 256 )). Default record size is 128.

Append is used to open a text file, placing the file position at the end, to allow for the addition of text to the file.

File I/O with typed files is just about the same. Declare a file of a structured type (i.e. F : file of CustRec), then do a Reset. To get to record number N, call Seek( F, N ), where the first record in the file is record 0. In the case of file I/O on untyped files, Seek( F, N ) will seek N records into the file based on the filesize passed when the file was opened.

Be aware that some of this information may be specific to Delphi, but I believe it to be applicable to modern "generic" Pascal compilers. (For instance, Delphi now uses AssignFile to replace Assign, as Assign is now a method call in objects).

[B]procedure[/B] CopyFile;
[B]var[/B]
  F1  : text;
  F2  : text;
  FN1 : [B]string[/B];
  FN2 : [B]string[/B];
[B]begin[/B]
  FN1 := 'infile.txt';
  FN2 := 'outfile.txt';

  assign( F1, FN1 );
  reset( F1 );  [I]{ open file for reading }[/I]

  assign( F2, FN2 );
  rewrite( F2 );  [I]{ open file for writing, destroying contents, if any }[/I]

  [B]while not[/B] EOF( F1 ) [B]do[/B]
  [B]begin[/B]
    readln( F1 );
    [I]{ file manipulation goes here }[/I]
    writeln( F2 );
  [B]end[/B];

  close( F1 );
  close( F2 );
[B]end[/B];

Thank you!!!

I am not having a specific problem with it now... But I didn't understand how to do it when i read it in the text.
Thank you!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.