Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, well, the following program opens a file and reads the first two strings that exist in the first line into two variables. The problem is that I’ve been trying to see what’s wrong with the following program, but I can’t see why it’s not working.
The compiler compiles it without any problem, but when I run it, it says exited with exitcode 2.
The problem is that the file exists.
What seems to be the problem?
Can anyone please help?

This is the code.
Delphi
Program num;

Var
f:Text;
b,g:String;
c:String[1];

Procedure thenum (a:String);
Begin
Assign(f,'textfileoffile.txt');
Reset(f);
a:='A';
while not eof(f) and (a<>' ') do
begin
Read (f,a);
End;
Writeln(a);
End;

Procedure sth ( j:String);
begin
Assign(f,'textfileoffile.txt');
Reset(f);
j:='A';
while not seekEoln and eof(f) do
begin
read(f,j);
end;
Writeln(j);
End;


begin
Assign(f,'textfileoffile.txt');
repeat
Reset(f);
until eof(f);
thenum (b);
read(f,c);
sth (g);
if eof(f)then
Close(f);
Readln;
End.
Posted
Updated 21-Jan-14 3:25am
v2

Quote:
repeat
Reset(f);
until eof(f);
This sequence looks wrong to me.
 
Share this answer
 
Comments
Member 10538783 21-Jan-14 9:42am    
Thanks for your help, but i tried to writ it in an another way but it still doesn't work.
CPallini 21-Jan-14 10:03am    
How did you write it?
Member 10538783 21-Jan-14 10:12am    
I wrote it like that, so as to open the file once, only in the main program,
there it is.
Program num;

Var
f:Text;
b,g:String;
c:String[1];

Procedure thenum (a:String);
Begin
Assign(f,'textfileoffile.txt');
Reset(f);
a:='A';
while not eof(f) and (a<>' ') do
begin
Read (f,a);
End;
Writeln(a);
End;

Procedure sth ( j:String);
begin
Assign(f,'textfileoffile.txt');
Reset(f);
j:='A';
while not seekEoln and eof(f) do
begin
read(f,j);
end;
Writeln(j);
End;


begin
Assign(f,'textfileoffile.txt');
repeat
Reset(f);
until eof(f);
thenum (b);
read(f,c);
sth (g);
if eof(f)then
Close(f);
Readln;
End.
I wrote it like that, so as to open the file once, only in the main program,
there it is.
Program num;

Var
f:Text;
b,g:String;
c:String[1];

Procedure thenum (a:String);
Begin
Assign(f,'textfileoffile.txt');
Reset(f);
a:='A';
while not eof(f) and (a<>' ') do
begin
Read (f,a);
End;
Writeln(a);
End;

Procedure sth ( j:String);
begin
Assign(f,'textfileoffile.txt');
Reset(f);
j:='A';
while not seekEoln and eof(f) do
begin
read(f,j);
end;
Writeln(j);
End;


begin
Assign(f,'textfileoffile.txt');
repeat
Reset(f);
until eof(f);
thenum (b);
read(f,c);
sth (g);
if eof(f)then
Close(f);
Readln;
End.
 
Share this answer
 
Comments
Richard MacCutchan 21-Jan-14 12:57pm    
You are opening and closing the file too many times, without any real logical reason. Get rid of your procedures and put all the code inline in sequence, something like:

Open the file
While NOT end of file
Begin
Read the next item
Display the item // this aids debugging
Process the item // do any extra work necessary
End
Close the file
Thank you very much, the program now, works without any problem at all.
 
Share this answer
 
Comments
enhzflep 22-Jan-14 12:17pm    
Rather than posting comments as Solutions, you should either
(a) hover over the post and hit the "Reply" button - do this to reply to comments
or
(b) Hit the "Have a Question or Comment?" button that follows the post - do this add a comment to Questions or Solutions.

For two reasons - the first is that is how it's done, the second reason is that otherwise the automated email informing someone of a reply to the post will not be sent. You should recieve notification of this comment in an email, as is the intention.

p.s - haven't coded in Pascal for over 20 years. What a fun trip down memory lane!
RaviRanjanKr 22-Jan-14 13:26pm    
Always use "Have Question or comment" to give feedback instead of posting as separate answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900