Recent

Author Topic: [Solved] unable to write onto a Textfile  (Read 1880 times)

sethgreen23

  • Newbie
  • Posts: 3
[Solved] unable to write onto a Textfile
« on: August 20, 2017, 05:27:26 pm »
Good Evening Everyone:
i wrote this procedure to read from a binary file and to write onto a text file when decision=2 and the code is like this:


Code: Pascal  [Select][+][-]
  1. type
  2.   Condidat=record
  3.      nom,prenom:string[20];
  4.      decision:1..3;
  5.   end;
  6.   con=File of Condidat;
  7. var
  8.   coni:con;
  9.   cona:TextFile;
  10.  
  11. procedure rangerajour(var c:con;var ca:TEXT);
  12. var
  13.   x:Condidat;
  14.   txt:string[255];
  15. begin
  16.  reset(c);
  17. {$I-}
  18.   Rewrite(ca);
  19. {$I+}
  20.  
  21.  while not(EOF(c)) do
  22.  begin
  23.     read(c,x);
  24.     with x do
  25.     begin
  26.       if decision=2 then
  27.       begin
  28.        try
  29.           if IOResult =0 then begin // OK, we can write the file
  30.           txt:='Nom: '+nom+' , Prenom: '+prenom ;
  31.           writeln(txt);
  32.           write(ca,txt);
  33.           end else begin
  34.             Writeln('error opening file for writing');
  35.           end;
  36.        finally
  37.            writeln('Problem Occured');
  38.        end;
  39.  
  40.  
  41.       end;
  42.     end;
  43.  end;
  44. end;
  45.  
  46. begin
  47.    assign(coni,'D:\bac\resultat.FCH');
  48.    AssignFile(cona,'D:\bac\ajour.txt');
  49.    //remplir(coni);
  50.    admis(coni);
  51.    rangerajour(coni,cona);
  52.    quit;
  53. end.

i tryed to run it just i got "Problem Occured" and it doesnt wrote onto the file .please any help
« Last Edit: August 20, 2017, 06:24:22 pm by sethgreen23 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: unable to write onto a Textfile
« Reply #1 on: August 20, 2017, 05:46:47 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   Condidat= packed record  // PACKED RECORD
  3.      nom,prenom:string[20];
  4.      decision:1..3;
  5.   end;
  6.  
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

sethgreen23

  • Newbie
  • Posts: 3
Re: unable to write onto a Textfile
« Reply #2 on: August 20, 2017, 06:07:42 pm »
i changed from a record to a packed record and i got the same thing "Problem Occured"  and it doesnt write onto the TextFile

sethgreen23

  • Newbie
  • Posts: 3
Re: unable to write onto a Textfile
« Reply #3 on: August 20, 2017, 06:21:59 pm »
Problem Solved The issue is that i forgot to close the file when i finished writing the lines onto it.

Code: Pascal  [Select][+][-]
  1. procedure rangerajour(var c:con;var ca:TextFile);
  2. var
  3.   x:Condidat;
  4.   txt:string[255];
  5. begin
  6.  reset(c);
  7. {$I-}
  8.   rewrite(ca);
  9. {$I+}
  10.  
  11.  while not(EOF(c)) do
  12.  begin
  13.     read(c,x);
  14.     with x do
  15.     begin
  16.       if decision=2 then
  17.       begin
  18.           if IOResult =0 then begin // OK, we can write the file
  19.             txt:='Nom: '+nom+' , Prenom: '+prenom ;
  20.             //writeln(txt);
  21.             writeLn(ca,txt);
  22.           end else begin
  23.             Writeln('error opening file for writing');
  24.           end;
  25.  
  26.  
  27.  
  28.       end;
  29.     end;
  30.  end;
  31. //i added this line and it is working fine now
  32. close(ca);
  33. end;
           

 

TinyPortal © 2005-2018