begin
read(f,c);
s:=s+c;
end;
Вы читаете символ/символ и добавляете строку, поэтому ваша программа работает медленно. Нельзя читать полный файл в одной переменной. Используйте буфер для хранения содержимого файла, затем обработайте его и освободите буфер для следующего ввода.
program ReadFile;
uses
Sysutils, Classes;
const
C_FNAME = 'C:\textfile.txt';
var
tfIn: TextFile;
s: string;
Temp : TStringList;
begin
// Give some feedback
writeln('Reading the contents of file: ', C_FNAME);
writeln('=========================================');
Temp := TStringList.Create;
// Set the name of the file that will be read
AssignFile(tfIn, C_FNAME);
// Embed the file handling in a try/except block to handle errors gracefully
try
// Open the file for reading
reset(tfIn);
// Keep reading lines until the end of the file is reached
while not eof(tfIn) do
begin
readln(tfIn, s);
Temp.Append(s);
end;
// Done so close the file
CloseFile(tfIn);
writeln(temp.Text);
except
on E: EInOutError do
writeln('File handling error occurred. Details: ', E.Message);
end;
//done clear the TStringList
temp.Clear;
temp.Free;
// Wait for the user to end the program
writeln('=========================================');
writeln('File ', C_FNAME, ' was probably read. Press enter to stop.');
readln;
end.
Другие примеры в File Handling in Pascal
Подсказка: конкатенации –
С помощью [ 'TStringList'] (http://www.freepascal.org/docs-html/rtl/classes/tstringlist.html) и его метод ['LoadFromFile'] (http://www.freepascal.org/docs-html/rtl/classes/tstrings.loadfromfile.html). Также узнайте больше о буферизованном чтении/записи файлов: ['BlockRead'] (http://www.freepascal.org/docs-html/rtl/system/blockread.html), [' BlockWrite'] (http: // www .freepascal.org/docs-html/rtl/system/blockwrite.html) и, конечно, ['TFileStream'] (http://www.freepascal.org/docs-html/rtl/classes/tfilestream.html) и [ 'TStringStream'] (http://www.freepascal.org/docs-html/rtl/classes/tstringstream.html) – Abelisto
Я поддержал этот вопрос. Это был законный вопрос, даже если эксперт-паскаль считает это немного глупым. – linuxfan