Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Using the OnlineGDB web interface can I read data from an external file rather than the Standard Input?

+2 votes
asked Jul 18, 2023 by Gary Ohm (450 points)
I would like to read text from and write text to external data files rather than the Standard Input and Output (stdout) that's on the bottom of web browser interface. I am using the pascal compiler and the Read(InData, parameter ..) statement where InData is defined as type Text in the var section.

The compiler gives the following error message:

Runtime error 2 at $00000000004016C6
  $00000000004016C6
  $000000000042360C

2 Answers

+1 vote
answered Jul 18, 2023 by Peter Minarik (82,640 points)
selected Jul 19, 2023 by Gary Ohm
 
Best answer

Working with text file is rather simple in Pascal. See the below example for first writing data to a text file, then reading it back from it.

program FileTest;
var
    outputFile: text;
    inputFile: text;
    line: String;
begin
    Assign(outputFile, 'test.txt');
    Rewrite(outputFile);
    WriteLn(outputFile, 'First line');
    WriteLn(outputFile, 'Second line');
    Close(outputFile);
    WriteLn('WRITING - All Done');

    Assign(inputFile, 'test.txt');
    Reset(inputFile);
    while not eof(inputFile) do
    begin
        ReadLn(inputFile, line);
        WriteLn('Line read: ', line);
    end;
    Close(inputFile);
    WriteLn('READING - All Done');
end.

For more details and how to use binary files, read here.

0 votes
answered Jul 19, 2023 by Gary Ohm (450 points)
edited Jul 19, 2023 by Gary Ohm
Thank you very much for your code example and showing how to use the 'Assign' statement. Your example was very helpful.
commented Jul 19, 2023 by Peter Minarik (82,640 points)
I'm glad you're sorted! :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...