PDA

View Full Version : IO Error.



Voltrox
21-10-2006, 03:14 AM
Hello,

I tried this code and Delphi 2006 gave me an IO Error 104:

procedure TForm1.Button1Click(Sender: TObject);
var test1: TextFile;
var rtext: System.string;
begin
AssignFile(test1, 'C:\test.txt');
ReWrite(test1);
while not Eof(test1) do
begin
ReadLn(test1, rtext);
ShowMessage(rtext);
CloseFile(test1);
end;
end;

Yet delphibasics.co.uk use it as if it's ok,

Robert Kosek
21-10-2006, 04:00 AM
Remove the rewrite call. :roll:

AthenaOfDelphi
21-10-2006, 07:26 AM
Remove the rewrite call. :roll:

More precisely, replace 'rewrite' with 'reset', and move the 'closefile' outside of the 'while...do' loop. At the moment, your code is creating the file and opening it for writing. I suspect the 104 is being raised by the EOF since EOF is only used when you've opened the file for random access with 'reset'.

A nice way of handling it would be this...


procedure TForm1.Button1Click(Sender: TObject);
var
test1: TextFile;
rtext: System.string;
begin
AssignFile(test1, 'C:\test.txt');
if fileExists('c:\test.txt') then
begin
reset(test1);
try
try
while not Eof(test1) do
begin
ReadLn(test1, rtext);
ShowMessage(rtext);
end;
except
on e:exception do
showMessage('An exception occured reading C:\test.txt. Message was:- '+e.message);
end;
finally
try
CloseFile(test1);
except
end;
end;
end;
end;


Append - Opens the file for writing and puts the file position at the EOF (Only works for text files)
Reset - Opens the file for random access (ie. you can read and write)
Rewrite - Creates the file and opens it for writing

Voltrox
27-10-2006, 04:53 AM
Ok, this is a different program, it keep giving me "IO Error 32"

var FACCR: TextFile;
begin
try
VACID1 := FIMANF1.MFIMANACNM1.Text;
if not DirectoryExists('C:\HXACC') then CreateDir('C:\HXACC');

MACCFNAM := 'C:\HXACC\' + VACID1 + '-' + 'MDF' + '.VTD';
if not FileExists(MACCFNAM) then FileCreate(MACCFNAM);

AssignFile(FACCR, MACCFNAM);
ReWrite(FACCR);
VUNITIME1 := Now;
VUNITIME2 := DateTimeToStr(VUNITIME1);
WriteLn(FACCR, VUNITIME2);
WriteLn(FACCR, '[ACCOUNT-ACTIVE]');
WriteLn(FACCR, VAMAIV);
CloseFile(FACCR);
HXDFLOCS1;
MFIMANUPD1;

could someone help me?

JernejL
27-10-2006, 05:24 AM
Just look into help what io error 32 means and at least try to solve it yourself :?

Voltrox
27-10-2006, 01:03 PM
I already did, I found that the problem is with ReWrite, because it won't execute any code after rewrite, but I have no idea of what to do to it...

Traveler
27-10-2006, 01:36 PM
At first I couldn't find the problem either. After I cleaned up your code a bit I realized that the filename you are using is not correct.



procedure TForm1.Button1Click(Sender: TObject);
var FACCR : TextFile;
VACID1 : string;
MACCFNAM : string;
VUNITIME1 : TDateTime;
VUNITIME2 : string;
VAMAIV : string;
begin
try
VACID1 &#58;= FIMANF1.MFIMANACNM1.Text; // <--- what is this ???
if not DirectoryExists&#40;'C&#58;\HXACC'&#41; then CreateDir&#40;'C&#58;\HXACC'&#41;;

MACCFNAM &#58;= 'C&#58;\HXACC\test-MDF.txt'; // replaced your string with a proper filename

AssignFile&#40;FACCR, MACCFNAM&#41;;
if not FileExists&#40;MACCFNAM&#41; then // check here if you need to create or open a new file
rewrite&#40;FACCR&#41;
else
Reset&#40;FACCR&#41;;
VUNITIME1 &#58;= Now;
VUNITIME2 &#58;= DateTimeToStr&#40;VUNITIME1&#41;;
WriteLn&#40;FACCR, VUNITIME2&#41;;
WriteLn&#40;FACCR, '&#91;ACCOUNT-ACTIVE&#93;'&#41;;
WriteLn&#40;FACCR, VAMAIV&#41;;
CloseFile&#40;FACCR&#41;;
except
//your exceptions here
end
end;

Voltrox
27-10-2006, 02:47 PM
Problem solved. :)

Voltrox
27-10-2006, 04:11 PM
oh, i already solved the problem, but thanks. :)

that looks even better. :)