Tạo File “Dayso.dat” có kiểu gồm các phần tử nguyên gồm 1000 số nguyên nhỏ hơn 1000 .
Đọc file này và sao chép các phần tử chẵn sang File dạng Text là “Sochan.TXT” , mỗi dòng chứa 10 số .
Uses Crt; Const Max = 1000; TenFi = 'Dayso.dat'; TenFo = 'Sochan.txt'; Type KFi = File of Integer; Var Fi : KFi; Fo : Text; Procedure Ghi1; Var i,x : Integer; Begin Assign(Fi,TenFi); {$I-} Rewrite(Fi); {$I+} If IoResult<>0 then Begin Writeln('Loi File'); Readln; Halt End; Randomize; For i:=1 to Max do Begin x := Random(1000); Write(Fi,x); End; Close(Fi) End; Procedure Ghi2; Var x,dem : Integer; Begin Assign(Fi,TenFi); {$I-} Reset(Fi); {$I+} If IoResult<>0 then Begin Writeln('Loi File'); Readln; Halt End; Assign(Fo,TenFo); {$I-} Rewrite(Fo); {$I+} If IoResult<>0 then Begin Writeln('Loi File'); Readln; Halt End; Dem := 0; While not eof(Fi) do Begin Read(Fi,x); If (x mod 2 =0) then Begin Write(Fo,x:5); Inc(dem); If (dem mod 10 =0) then Writeln(Fo) End; End; Close(Fi); Close(Fo) End; BEGIN Clrscr; Ghi1; Ghi2; Readln END.
Advertisements
Thảo luận
Không có bình luận