Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File I/O error 6 while reading an empty File of Record

Status
Not open for further replies.

glacialfury

Programmer
Apr 17, 2005
3
US
For the record, I'm not very experienced in programming in general and with Delphi in particular; hence the poor practice of using goto's, so please be gentle :).

I have a program that, upon loading, checks to see whether a certain file exists. If it does not exist, an empty one is created; if it does exist, it should be loaded into the program.

The creation of the empty file seems to work; however, when the program loads and tries to read that empty file, I get an I/O Error 106.

If it is important, the file is a typed file for records; the records will contain three strings each.

Here is the code for the offending procedure; I have marked off the area that is causing the area. I have tested it thoroughly to ensure that this is indeed the spot that is causing trouble.

The error-producing section is toward the bottom of the procedure.

*********************************************************
procedure Tform_main.FormCreate(Sender: TObject);
var tmpServer : ServerInfo;

label assignPaths;
label noPaths;
label createEmptyServerList;
label reloadServerList;
label dealwithServerList;

begin
if (FileExists('paths.txt'))
then goto assignPaths
else goto noPaths;

assignPaths:
begin
//loads paths.txt into variables for use
AssignFile(pathsFile,'paths.txt');
Reset(pathsFile);
ReadLn(pathsFile,Unit4.clientPath);
ReadLn(pathsFile,Unit4.sclientinfoPath);
CloseFile(pathsFile);
goto dealwithServerList;
end;
Exit;

noPaths:
begin
self.btn_AddServer.Enabled := false;
self.btn_RemoveServer.Enabled := false;
self.btn_Go.Enabled := false;
goto dealwithServerList;
end;
Exit;

dealwithServerList:
if (FileExists('servers.dat') = false)
then goto createEmptyServerList
else goto reloadServerList;

createEmptyServerList:
begin
AssignFile(ServerList,'servers.dat');
ReWrite(ServerList);
CloseFile(ServerList);
end;
Exit;

//*****************************************
// THIS SECTION HAS PROBLEMS - ERROR 106 *
// ERROR 106 Invalid numeric format *
//*****************************************

reloadServerList:
begin
ShowMessage('Reloading old Server List');
AssignFile(ServerList,'servers.dat');

while not EOF do
begin
Reset(ServerList);
Read(ServerList,tmpServer);
end;
CloseFile(ServerList);
end;
Exit;


end; //end of procedure
 
try this :

Code:
 reloadServerList:
      begin
        ShowMessage('Reloading old Server List');
        AssignFile(ServerList,'servers.dat');
        Reset(ServerList); 
        while not ServerList.EOF do
        begin
          Read(ServerList,tmpServer);
        end;
        CloseFile(ServerList);
      end;


--------------------------------------
What You See Is What You Get
 
Thank you for the quick response; however, that gives me a compile error:

[Error] Unit1.pas(241): Record, object or class type required

To clarify:
ServerInfo is the name of the record type.
ServerList is a File of ServerInfo.


For the record, I am using Delphi 7.
 
jups correct earthandfire, sorry for that.

glacialfury, if you're using D7, look into the use of Tfilestream or even TstringList (and use the LoadFromFile method)

cheers

--------------------------------------
What You See Is What You Get
 
Thank you very much, earthandfire and whosrdaddy. It does not give me the error any more. Now I have a couple of access violations from some other code to figure out, but you got my big puzzler out of the way. :-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top