Board index » delphi » Delphi I/O

Delphi I/O

I'm having a problem with what should be a piece of cake.... ESPECIALLY
since it's what I just got finished doing a month or two ago in school...
 >:^/

I'm using Delphi 2 and I have two programs... one that writes to a file,
and another that reads the file written by the first one.  The writer
program (I'll call it #1) writes to file as a record type that I defined.
 The reading program (I'll call it, ummm... #2) reads in the file as the
same, defined record type. The code is as follows...

{-------PROGRAM #1-------}
procedure TfrmMain.cmdSendClick(Sender: TObject); type
ErrorList = record
ComputerName : String[10];
OpName : String[15];
Shift : String[3];
Time : string[6];
AMPM : String[2];
Description : String[200];
end;
ErrorFile = file of ErrorList;
var
ProblemInfo : ErrorList;  //CURRENT ERROR INFO
ProblemFile : ErrorFile;  //FILE TO APPEND TO FOR PROG #2
WritePath : string;

begin //WRITE TO FILE// WritePath :=
ExtractFilePath(Application.ExeName);  //FIND APP PATH
assignfile(ProblemFile, WritePath + 'Problems.txt'); Append(ProblemFile);
ProblemInfo.ComputerName := frmMain.caption; ProblemInfo.OpName :=
txtName.text; ProblemInfo.Shift := cmbShift.text; ProblemInfo.Time :=
txtTime.text; ProblemInfo.AMPM := cmbTime.text; ProblemInfo.Description
:= txtDescription.text; write(ProblemFile,ProblemInfo);  //WRITE THE
PROBLEM INFO TO FILE ... end;

{----PROGRAM #2----}
procedure TfrmTileAlertMain.shpBookerMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
type
ErrorList = record
ComputerName : String[10];
OpName : String[15];
Shift : String[3];
Time : string[6];
AMPM : String[2];
Description : String[200];
end;
Errors = file of ErrorList;

var
ErrorReport : Errors;   //FILE TO READ FROM
ListOfErrors : ErrorList; //INFO RETRIEVED FROM FILE

begin assignfile(ErrorReport,'L:\TileAlert\Problems.txt');
reset(ErrorReport); while not EOF(ErrorReport) do begin
read(ErrorReport,ListOfErrors);  //READ FILE WRITTEN BY PROG #1..ERROR IS
HERE frmTileAlertMain.Caption := ListOfErrors.OpName //JUST TESTING end;
closefile(ErrorReport) end;

{------END OF PROGRAMS-----}

When program #2 is run and it reads from the file, it errors out with
"Read beyond end of file."  The program continues, and if I try again,
this time I get "I/O Error 32."  If I evaluate the values (ListOfErrors)
read in from the data file (ErrorReport) with the "Evaluate/Modify"
window, I see that all values appear correct.

I've tried making the last string variable smaller, adding a dummy
integer field at the end of the record, and several other desperate
attempts to make the program work.  If anyone has any suggestions, please
let me know! Also, if you post a reply, please email me a copy.

Thanks in advance,
Johnny Allen

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet

 

Re:Delphi I/O


Quote
>When program #2 is run and it reads from the file, it errors out with
>"Read beyond end of file."  The program continues, and if I try again,
>this time I get "I/O Error 32."  If I evaluate the values (ListOfErrors)
>read in from the data file (ErrorReport) with the "Evaluate/Modify"
>window, I see that all values appear correct.

>I've tried making the last string variable smaller, adding a dummy
>integer field at the end of the record, and several other desperate
>attempts to make the program work.  If anyone has any suggestions, please
>let me know! Also, if you post a reply, please email me a copy.

One suggestion is to use an INI File object (TIniFile).  This makes it easy
to have "history" on the records and an easy way to get in, find what you
want, and get out.  Makes for a slightly easier way to get at your
information and allows Delphi to manage the file.

Re:Delphi I/O


Quote
Johnny_Al...@shawinc.com wrote:

You have a terrible lay-out in your code, but from what I could decipher
from it I would say that with writing the file you forget to close it
(using closefile). Typically this will trash the last record written.

Regards,

Remco de Korte
Soft Machine
(download SNAKES at http://www.xs4all.nl/~remcodek/download.html)

Other Threads