Solved

Delphi thinks it's EOF

Posted on 1998-01-12
21
803 Views
Last Modified: 2012-06-21
Hi ...
  My app reads plain text files. In one of these text files, there happened to be a weird looking character - it looked like a vertical bar type thing. It was around the beginning of the file and the file was quite large.When my program reached this funny character, it stopped reading the file and appeared to take this character as EOF??? Ever see this before. How do I prevent something like this from happenning?

Cheers
  Shawn Halfpenny
   drumme59@sprint.ca
0
Comment
Question by:aztec
  • 12
  • 6
  • 2
  • +1
21 Comments
 
LVL 1

Expert Comment

by:Odie
ID: 1356234
If it's a plain text file, I think CTRL + Z means EOF.  Maybe you can prevent this by reading it as a binary file.
0
 
LVL 4

Expert Comment

by:itamar
ID: 1356235
Where did this specific file come from ?
If it's suposed to be plain-text Why there is special characters in it ?

0
 
LVL 2

Accepted Solution

by:
mvz121697 earned 50 total points
ID: 1356236
Don't trust the EOF character (26) as begin the end of the file.
Instead use the FileSize function, to get the size of the file.
See the example from the Delphi help-file:

var
   f: file of Byte;
   size : Longint;
   S: string;
   y: integer;
 begin
   if OpenDialog1.Execute then begin
     AssignFile(f, OpenDialog1.FileName);
     Reset(f);
     size := FileSize(f);
     S := 'File size in bytes: ' + IntToStr(size);
     y := 10;
     Canvas.TextOut(5, y, S);
     y := y + Canvas.TextHeight(S) + 5;
     S := 'Seeking halfway into file...';
     Canvas.TextOut(5, y, S);
     y := y + Canvas.TextHeight(S) + 5;
     Seek(f,size div 2);
     S := 'Position is now ' + IntToStr(FilePos(f));
     Canvas.TextOut(5, y, S);
     CloseFile(f);
   end;
 end;
end.

0
 

Author Comment

by:aztec
ID: 1356237
Hello..
   Regarding Odie's comment...how do I read a text file as a binary file?
  Regarding Mvz's answer, my file is not file of Byte, but is text. Will FileSize still work? And furthermore, how do I use FileSize to determine I'm at end-of-file? Like this:

if FileSize(f) = FilePos(f)...

..is that correct?

Cheers
  Shawn
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1356238
FileSize(f) = FilePos(f) should do the trick.

If there are EOF-cahrs in the middle of the file, it is not an text-file. You should read it as a binary file.

Here's an example of working with binary files:
( copy source file to destination file )


function FileCopy(var source, destination: String; EQuery: boolean):
boolean;

implementation

uses
    SysUtils, Dialogs;

function FileCopy(var Source, Destination: String; EQuery: boolean):
boolean;
var
   Src, Dest, len: Integer;
   size: LongInt;
   buffer: array [0..4095] of Byte;
begin
     if Not FileExists(Source) then begin
        ShowMessage('Source File Not Found');
        Result := False;
        exit;
        end;

     if FileExists(Destination)then begin
        { dest file exists, if equery opt for rename }
        if EQuery then  begin
           { future: rename destination file }
           Result := False;
           exit;
           end
        else begin
             { copy over old existing file }
             DeleteFile(Destination);
        end;
     end;

     { copy source to destination }
     Result := False;
     if Source <> Destination then begin
         Src := FileOpen(source, fmOpenRead);
         if Src >= 0 then begin {successfully opened }
             { get file size }
             size := FileSeek(Src, 0, 2);
             FileSeek(Src, 0, 0);

             { create new file }
             Dest := FileCreate(Destination);
             if Dest >= 0 then begin   { successful creation }
                while size > 0 do begin
                      len := FileRead(Src, buffer, sizeof(buffer));
                      FileWrite(Dest, buffer, len);
                      size := size - len;
                end;

                { keep date and attribute values }
                FileSetDate(Dest, FileGetDate(Src));
                FileClose(Dest);
                FileSetAttr(Destination, FileGetAttr(Source));

                Result := True;
             end;
             Fileclose(Src);
         end;
        end
     else begin
         { attempted to copy source to itself }
         ShowMessage('FILE COPY ERROR: Cannot Copy File to itself');
     end;
end;
0
 

Author Comment

by:aztec
ID: 1356239
You guys leave a lot of unanswered questions :

(1) If I use the Fileseek and Filepos things to determine EOF, then my file has to be a File of Byte, not a text file, right? Then how do I read records from a File of Byte? Does "readln" work?

(2) If I convert my text file to binary - as mvz outlines - then what do I do then? How do I read it? With "readln" ?

Regards,
  Shawn Halfpenny
  drumme59@sprint.ca
0
 
LVL 4

Expert Comment

by:itamar
ID: 1356240
I still don't understand why a text-file has EOF inside...
0
 

Author Comment

by:aztec
ID: 1356241
Itamar... I know...it shouldn't, but sometimes some crap gets in text files for no apparent reason. I want to have my program be on the lookout for that.

Shawn
0
 

Author Comment

by:aztec
ID: 1356242
mvz...are you there???

Shawn Halfpenny
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1356243
The 'crap' you get in your text files, is the reason why you DON'T have text-files. In my opinion the defition of a text-file is a file where there are no bytes below value 32 and above 128.
Only 13 (carriage return) and 10 (Line feed) are allowed.
If you have (even 1) bytevalue below 32 (and you are talking of the EOF, which is 26), you have a binary file, and you should thread it as one.
A big disadvantage is that you cannot use readln, but you have to use fileread(). This function reads in a predefined number of caracters. Be aware that CR & LF caracters are threated as 'normal' caracters and could be in the middle of your buffer.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:aztec
ID: 1356244
Couldn't I use the BlockRead procedure instead of the fileRead() function? Then I could read in one record at a time, analyze its contents checking for weird characters, clean it up if it contains any, then output it using BlockWrite?
0
 

Author Comment

by:aztec
ID: 1356245
Am trying the BlockRead thing and am following the example in the on-line help faithfully, but still get a "type mismatch" error...here's my code:

procedure cleanfile(infilestr:string; outfilestr:string);
label doagain, ckat;
var
  fromf : File;
  hinrec: array[1..2048] of Char;
  inrec: array[1..2048] of Char;
  numread, numwritten : word;
  x,i, startindex, endindex : integer;
  foundbad : boolean;
  hstr : string;
begin

  assignfile(fromf, infilestr);
  reset(fromf, 2048);

  assignfile(outfile, outfilestr);
  rewrite(outfile);

  repeat
    BlockRead(fromf, hinrec, 1, numread);
    inrec := strlower(hinrec);

.. this statement (inrec:=strlower(hinrec)) generates a type mismatch error. What am I doing or declaring wrong? I pretty well follow exactly what the "BlockRead" example says in the online help!

Cheers
   Shawn


0
 

Author Comment

by:aztec
ID: 1356246
mvz...are you there???

Shawn
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1356247
I think you should not use BlockRead, unless you are sure that your files-size are always multiple's of your blocksize.

Here's my Cleanfile suggestion:
(It deletes EOF-karakters and converts upper to lowercase)

function cleanfile(infilestr:string; outfilestr:string):boolean;
var i,Src,Dest,size,len,Count:integer;
    bufferIn,bufferOut:array[0..2048] of Byte;
begin
result:=false;
Src := FileOpen(infilestr, fmOpenRead);
try
 if Src >= 0 then begin
   size := FileSeek(Src, 0, 2); //filesize
   FileSeek(Src, 0, 0);        // goto top
   Dest:=FileCreate(outFileStr);
   try
   if Dest> 0 then begin
      while size > 0 do begin
        len := FileRead(Src, bufferIn, sizeof(bufferIn));
        Count:=0;
        for i:=0 to len-1 do begin
           if bufferIn[i]<>26 then begin
                if (Chr(bufferIn[i])>='A') AND (Chr(bufferIn[i])<='Z') then
                bufferOut[Count]:=bufferIn[i]+32 else
                bufferOut[Count]:=bufferIn[i];
                Inc(Count);
           end;
        end;
        if Count>0 then FileWrite(Dest, bufferOut, Count);
        size := size - len;
      end;
      result:=true;
   end;
   finally
     FIleClose(Dest);
   end;
 end;
finally
 FIleClose(Src);
end;
end;

0
 

Author Comment

by:aztec
ID: 1356248
thanks for the function mvz, but I need to use BlockRead (I think) because I must read in a record (record length is variable) at a time and BlockRead seems to allow for this (does FileRead?). If I use FileRead, then I will wind up splitting records by reading in a fixed number of bytes (2048) at a time. Do you see what I mean? Can you please explain to me why I get a type mismatch error on the line:

inrec := strlower(hinrec);

..as I mentioned before when trying to use BlockRead?
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1356249
FileRead does allows the number of bytes to read (3th paramter).
It even reports back the exactly read number of bytes (Blockread does not). I think this is important when reaching the end of the file.

StrLower does expect a string as parameter, not an array of char (which hinrec is). Therefor DElphi reports an error.
Instead you can use something like:

for i:=1 to 2048 do
    if (Chr(hinrec[i])>='A') AND (Chr(hinrec[i])<='Z') then
          hinrec[i]:=hinrec[i]+32

Greetings,
MvZ
0
 

Author Comment

by:aztec
ID: 1356250
you're not following me... I KNOW that FileRead has a parameter to read a set # of bytes...that's why I don't think it'll work for me. Reading a fixed # of bytes will split up my records which I MUST NOT do! Therefore I think I have to use BlockRead - which you can set to read 'count' records at a time. Do you see what I'm saying?

Can you please explain to me how to use BlockRead?

Also, you say the function 'strlower' expects a string variable. Accoring to Delphi on-line help, this is the definition of 'strlower':

StrLower converts a string to lowercase.

Unit

SysUtils

Category

string handling routines (null-terminated)

function StrLower(Str: PChar): PChar;

Description

The StrLower function converts Str to lowercase and returns Str.

.. according to that, Strlower uses a PChar array as input, not a string. Can you clarify?
0
 

Author Comment

by:aztec
ID: 1356251
mvz...are you there????
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1356252
Sorry for the delay,
I was wrong with the Strlower-parameter.
It's in the return value. Use something like this:

StrCopy(inrec,StrLower(hinrec))

Be aware that inrec is still not a string. You can however let delphi convert it to one, by:

var cString:string;

cString:=inrec;

Now cString is a normal string;

Greetings
MvZ
0
 

Author Comment

by:aztec
ID: 1356253
MVZ...still waiting for an explanation on how to use BlockRead to read in 'Count' records at a time. In my case, 'Count' will be 1.

Shawn
0
 

Author Comment

by:aztec
ID: 1356254
mvz..you there????
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now