I want to learn how to read from files in Pascal. I read the literature, but I did not find simple actions like readln (). Explain as simple as possible. Svayal govnokod, knocks error (error 100):

program 1; var f:FILE of STRING; a:STRING; begin Assign(f,'tct.txt'); rewrite(f); read(f,a); writeln(a); readln; end. 

Who can explain (if possible and how to write to the file)

Task: read the text from the .dat file and create a file with the result

    1 answer 1

    To work with text files in Pascal there is a special type: TEXT.

     program reader; var f : Text; a : string; begin assign(f, "fileName.ext"); { связываем переменную с файлом } reset(f); { открываем для чтения } while not eof(f) do begin { пока не конец файла } read(f, a); { читаем из файла } writeln(a); { выводим на экран } end; close(f); { закрываем файл } end. program writer; var f : text; a : string; begin assign(f, "fileName.ext"); { связываем переменную и файл } rewrite(f); { открываем для записи (файл будет перезаписан, для дозаписи нужно использовать append(f); } write(f, a); { пишем в файл } close(f); { закрываем файл } end. 
    • thank! but: 1) what was the error 2) how to create a NEW FILE? 3) what if the .dat file? - dddkk
    • @iamqwerty: rewrite will create a new file if it does not already exist. If there is, it will get stuck. Will work with any extension, of course. The error was that rewrite opens the file for writing, not for reading. So read it would not work. - VladD
    • not TEXT but TEXTFILE The Text type is already obsolete and is equivalent to the TextFile type. [A good online reference for Delphi] [1] [1]: delphibasics.ru - vdk company
    • Thanks, all worked well! - dddkk