How to write procedure's content to a text file? lazarus / 9-Editor ---------------------------------------------------------------------- Spiller · 2014-01-08 20:54 Hello, Im all new to Pascal language (or any programming language actually). I need to make a project for uni and it has to write something to a text file. So I have a procedure, which calls out other procedures, which are calculations and the procedures, which are being called out by the first procedure, ouput text on the screen. But I need them to be written to a text file aswell. My question is how to type these calculation results to a text file? Is it even possible? At the moment I got this: Code: [Select]Procedure Arvutus;            begin                writeln('Algandmed on nyyd sisestatud, j2rgnevalt v2ljastatakse arvutustulemused:');                writeln;                Delay(1000);                SiirdeTakistus;                writeln;                Delay(1000);                Baasivool;                writeln;                Delay(1000);                Kollektorvool;                writeln;                Delay(1000);                Toitepinge;                writeln;                Delay(1000);                KollektorTakistiPinge;                writeln;                Delay(1000);                KollektorTakisti;                writeln;                Delay(1000);                EmitterPinge;                writeln;                Delay(1000);                EmitterTakisti;                writeln;                Delay(1000);                Baasipinge;                writeln;                Delay(1000);                TakistiR1;                writeln;                Delay(1000);                TakistiR2;                writeln;                Delay(1000);                EmitterKondensaator;                writeln;                Delay(1000);                SisendkondensaatoriImpedants;                writeln;                Delay(1000);                V2ljundkondensaatoriImpedants;                writeln;                Delay(1000);                readln;           end; Procedure V2ljastusFaili;            begin                assignfile(fail2, 'Arvutustulemused.txt');                 rewrite(fail2); //loob faili                writeln(fail2,[color=red]Arvutus[/color]);                close(fail2);           end; It wont work like that, should I make the "procedure Arvutus" into a function? But then I dont know how I define these procedures into variables .... Thanks in Advance! ---------------------------------------------------------------------- skalogryz · 2014-01-08 21:01 Just change V2ljastusFaili to the following: Code: [Select]Procedure V2ljastusFaili;            begin                assignfile(output, 'Arvutustulemused.txt');                 rewrite(output);                Arvutus;                close(output);           end; and you don't need these Delays() ---------------------------------------------------------------------- Spiller · 2014-01-08 21:13 I changed it. Now it just prints the calculation results on the screen, but still doesnt print them to the file. Additional info: fail2 is a variable of "textfile" ---------------------------------------------------------------------- skalogryz · 2014-01-08 21:22 Quote from: Spiller on January 08, 2014, 10:13:18 pmI changed it. Now it just prints the calculation results on the screen, but still doesnt print them to the file. are you calling V2ljastusFaili anywhere in your program? and you don't need fail2 variable anymore. ---------------------------------------------------------------------- Spiller · 2014-01-08 21:29 Yes, in the main part of the program. I let the user choose A - print results on screen and B - print results to file. Code: [Select]begin    Logo;        repeat              Sisestus;        until (valik = 'A') or (valik = 'a') or (valik = 'B') or (valik = 'b');        case valik of             'A','a':begin                          AndmedFailist;                          readln;                          end;             'B','b':begin                          K2sitsiSisestamine;                          readln;                          end;        end;   writeln('Parameetrid on ',pingev:0:0,',',vooluv:0:0,',',stakistus:0:0,',',sageduspiir:0:0);   readln;          repeat                V2ljastamine;          until (valik2 = 'A') or (valik2 = 'a') or (valik2 = 'B') or (valik2 = 'b');          case valik2 of               'A','a':begin                            Arvutus;                            readln;                            end;               'B','b':begin                            V2ljastusFaili;                            writeln('Arvutustulemused kirjutati faili "Arvutustulemused.txt"!');                            end;          end;   writeln;   writeln('V2ljumiseks vajutage "Enter" klahvi!');   readln; end. V2ljastamine is the procedure, which makes the user choose where to print the results. EDIT: After replacing "fail2" with "output", i get "RunError(103)", which is "File not open". ---------------------------------------------------------------------- skalogryz · 2014-01-08 21:39 ah... ok, so here's how V2ljastusFaili should looklike: Code: [Select]Procedure V2ljastusFaili;            begin                assignfile(output, 'Arvutustulemused.txt');                 rewrite(output);                Arvutus;                close(output);                output:=StdOut;           end; and please remove the last readln from Arvutus procedure; ---------------------------------------------------------------------- Spiller · 2014-01-08 21:58 Thanks alot ! I really appreciate it ! And if I have this kind of loop: Code: [Select]                       repeat                              writeln('Sisestage v6imendi pingev6imendus: ');                              readln(KasSis);                              val(KasSis,vooluv,error);                        until error = 0; It asks the user to input the "amplifier's voltage ampilfication" and if the input is a string, it will ask to enter the input again. Also the amplification cant be zero, so I wouldnt later get dividing by zero in the calculations. How should I modify the loop? Is the val thing even appropriate for this kind of loop or should I use repeat ... until loop to check for input not to be zero, but then again, how could I use the loop to check input for charactes etc ? ---------------------------------------------------------------------- skalogryz · 2014-01-09 00:18 Code: [Select]                       repeat                              writeln('Sisestage v6imendi pingev6imendus: ');                              readln(KasSis);                              val(KasSis,vooluv,error);                        until (error = 0) and (vooluv <> 0); ---------------------------------------------------------------------- Spiller · 2014-01-09 11:11 Thanks ! ---------------------------------------------------------------------- Spiller · 2014-01-10 14:57 Also If im reading from a file then, how can I make it check for the presence of any data? Code: [Select]Procedure AndmedFailist1;           //programm loeb failist andmed           begin                assignfile(fail, 'Algandmed.txt');                reset(fail);                readln(fail, tekstirida);                pingev := tekstirida;                writeln('Pingev6imendus on : ',pingev:0:0);                readln(fail,tekstirida);                vooluv := tekstirida;                writeln('Vooluv6imendus on : ',vooluv:0:0);                readln(fail,tekstirida);                stakistus := tekstirida;                writeln('Sisetakistus on : ',stakistus:0:0);                readln(fail,tekstirida);                sageduspiir := tekstirida;                writeln('Sageduspiir on : ',sageduspiir:0:0);                close(fail);           end;              I have made it so that each line will be defined to a certain name(pingev, voolu etc), the data in the file is :Real. But how can I make the program to give me a error (without the program crashing), if there isnt anything written in the file? Thanks if advance !