So... how do I use the program?
when the main program begins
we clear the screen,
set fname and tempname
create old file (this is the main)
write_in = you can write some lines to this file
when you press the '*' char then the procedure
closes the old.txt file
and then comes the read_and_copy
open it for reading( the old.txt file)
and if it is contains empty line then do nothing
just continue the reading else ,if the line isn't
empty then write in the temp file named new.txt
when all of not empty lines are copied then
we delete the main file (old.txt)
and rename the new.txt to old.txt
and last if you look at the old.txt file in the
bin directory then you will see that it isn't
contains empty lines...
if you want to read this file contents under pascal then
write a procedure that read a file back
procedure read_in_back(var f:text; name:string);
var li:string;
begin
assign(f,name);
reset(f);
while not eof(f) do begin
readln(f,li);
writeln(li);
end;
close(f);
readln;
end;
(*to call this procedure
read_in_back(f,'old.txt');
*)
ok,now let's see the main program again.
program without_empty_lines_2;
uses crt;
var f,temp:text;
s,fname,tempname:string;
(*text file maker procedure*)
procedure create(var x:text; xname:string);
begin
assign(x,xname);
rewrite(x);
close(x);
end;
(*write in some line the main text file*)
procedure write_in(var x:text; xname:string);
var tempstring:string;
e:char;
begin
e:='*';
assign(x,xname);
append(x);
tempstring:='';
writeln('Press ''*'' to quit.');
while tempstring <> e do begin
write('The words: ');
readln(tempstring);
if tempstring = e then break;
writeln(x,tempstring);
writeln('Press ''*'' to quit.');
end;
close(x);
end;
(*read and copy not empty lines*)
procedure read_and_copy(var x,y:text; xname,yname:string);
var s1:string;
begin
assign(x,xname);
create(y,yname);
append(y);
reset(x);
while not eof(x) do begin
readln(x,s1);
if (length(s1)=0) then continue
else writeln(y,s1);
end;
close(x);
close(y);
end;
(*reads back all of contents of a text file*)
procedure read_in_back(var f:text; name:string);
var li:string;
begin
assign(f,name);
reset(f);
writeln('the contents of the file are: ');
while not eof(f) do begin
readln(f,li);
writeln(li);
end;
close(f);
readln;
end;
begin (*main*)
clrscr;
fname:='old.txt';
tempname:='new.txt';
create(f,fname);
write_in(f,fname);
read_and_copy(f,temp,fname,tempname);
erase(f); (*delete old file*)
rename(temp,fname); (*and rename new file to old*)
read_in_back(f,fname);
readln;
end.
(*created by FlamingClaw.2010.03.17.Hungary*)