Solved

exception on Rewrite(DataFile)

Posted on 2004-03-30
15
1,226 Views
Last Modified: 2008-03-10
Hi,

My application refuses to do a rewrite(datafile) in...

procedure TNotePool.Save(FileName : string);
var
   DataFile   : TextFile;
   i          : integer;
begin
   AssignFile(DataFile, FileName);
   Rewrite(DataFile);

...when the datafile is not new.

The datafile is created by the same application. In a previous version (working correctly), probably compiled using Delphi 4 or 5, decimal instead of hexadecimal field and line separators were used. In the new version all decimal characters have been exchanged for hexadecimal. This somehow may explain why a datafile created by the older version application/delphi has lines like these...:

0ÿNew Noteÿÿÿ-1ÿ80ÿ50ÿ65535ÿ16ÿ0ÿ1ÿ10ÿaaaþ
bbbþ
cccÿ-1

...where in the later version (I use Delphi 7) they read like this...:

0ÿNew Noteÿÿÿ-1ÿ80ÿ50ÿ65535ÿ16ÿ0ÿ1ÿ10ÿaaaþbbbþcccÿ-1

The new application determines which kind of datafile is to be read and continues loading the file with no errors.

Now I strongly suspect that the unwillingness of the new application to Rewrite(DataFile) has to do with this. It happens not only when an old datafile is (attempted to be) saved, but also a new one.

Any guesses?

Thanks,
Ron dW
0
Comment
Question by:Ron_de_Weijze
15 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
?? missing the point to the problem

do you have problems to open the file?
or
do have problems to convert/parse the content?

meikl ;-)
0
 

Author Comment

by:Ron_de_Weijze
Comment Utility
Hi Meikl,

No problem to open or load the file, nor to convert/parse the content, but to write it back to the file. Saving it as a new file does not raise an exception but saving it as an existing file (the one opened or a different one) does.

Ron dW
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
the file may be still opened by yourself,
did you close the file?
0
 

Author Comment

by:Ron_de_Weijze
Comment Utility
Yes I did.
0
 
LVL 11

Assisted Solution

by:shaneholmes
shaneholmes earned 80 total points
Comment Utility
maybe close the file

and process all the messages and then attempt to rewrite

Application.ProcessMessages

Just thinking out load

shane
0
 
LVL 11

Assisted Solution

by:calinutz
calinutz earned 80 total points
Comment Utility
Delphi (and any other pr lang) has a a strong problem rewritinf read only files... I encountered a lot of times this problem and everytime I figured out that the file remains open somewhere in the process. So you should read carefully the code of your app. Maybe write a new app that only deals with the file... to keep a good perspective of the actions. Thats all I can say considering that there is no code  from you to analyze...
Cheers
0
 

Author Comment

by:Ron_de_Weijze
Comment Utility
Thank you for your comments and suggestions. I will try them out and let you know asap.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Ron_de_Weijze
Comment Utility
Dear Experts,

This is where the exception is raised. Messages were processed just before in the calling procedure.


procedure TNotePool.Save(FileName : string);
var
   DataFile   : TextFile;
   i          : integer;
begin
   AssignFile(DataFile, FileName);
//     <------------------------------- added commentline causes jump to except, not Rewrite!
   Rewrite(DataFile);

   for i := 0 to Count - 1 do
   begin
//      WriteLn(DataFile,
//              i, #255,
//              TNoteItem(Item[i]).Caption, #255,
//              TNoteItem(Item[i]).ImageFile, #255,
//              TNoteItem(Item[i]).IconFile, #255,
//              TNoteItem(Item[i]).NoteType, #255,
//              TNoteItem(Item[i]).Width, #255,
//              TNoteItem(Item[i]).Height, #255,
//              TNoteItem(Item[i]).Color, #255,
//              TNoteItem(Item[i]).IconSize, #255,
//              TNoteItem(Item[i]).IconLocation, #255,
//              integer(TNoteItem(Item[i]).Transparent), #255,
//              TNoteItem(Item[i]).TextSize, #255,
//              KillCRs(TNoteItem(Item[i]).Memo), #255,                   { ATTENTION : TEMPORARILY }
//              m_SheetPool.m_ItemList.IndexOf(TNoteItem(Item[i]).AggItem)
//             );
       WriteLn(DataFile,
              i, #$FF,
              TNoteItem(Item[i]).Caption, #$FF,
              TNoteItem(Item[i]).ImageFile, #$FF,
              TNoteItem(Item[i]).IconFile, #$FF,
              TNoteItem(Item[i]).NoteType, #$FF,
              TNoteItem(Item[i]).Width, #$FF,
              TNoteItem(Item[i]).Height, #$FF,
              TNoteItem(Item[i]).Color, #$FF,
              TNoteItem(Item[i]).IconSize, #$FF,
              TNoteItem(Item[i]).IconLocation, #$FF,
              integer(TNoteItem(Item[i]).Transparent), #$FF,
              TNoteItem(Item[i]).TextSize, #$FF,
              KillCRs(TNoteItem(Item[i]).Memo), #$FF,                  
              { ATTENTION : TEMPORARILY }
              m_SheetPool.m_ItemList.IndexOf(TNoteItem(Item[i]).AggItem)
             );
  end;

   CloseFile(DataFile);
end;


The comment line actually raises the exception, so the problem seems to be with the aftermath of assignfile rather than with rewrite..

0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
what for an exception do you get??
0
 

Author Comment

by:Ron_de_Weijze
Comment Utility
This is the exception:

...
  try
      // Save Data Files and Register
      m_Container.Save(FileName);
      ChDir(ExtractFilePath(FileName));
      SetLastProject;
   except
      showmessage('An error occurred while trying to save the current project!'); //<----
   end
...

procedure TContainer.Save(FileName : string);
var
   Extension : string;
   PureName  : string;
   Path      : string;
begin
   Extension := ExtractFileExt(FileName);
   PureName := RipExtension(ExtractFileName(FileName));
   Path := ExtractFilePath(FileName);

   // Store path for further use
   m_CurProjectDir := Path;

   // Extension of datafiles Hard-Coded .dat
   Extension := '.dat';

   // Create Directory if not existing
   if not SetCurrentDir(Path) then
      if not CreateDir(Path) then
         MessageDlg('Error Creating Directory', mtError, [mbOk], 0);

   m_NotePool.Save(Path + PureName + '_Not' + Extension);

end;
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
could you change temporary this

>except
>    showmessage('An error occurred while trying to save the current project!'); //<----
>end

into

except
   on e: exception do
       showmessage('An error occurred while trying to save the current project!'+#10+
                            'DetailInfo: '+e.message); //<----
   end;
end

and tell then the DetailInfo

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
btw. typos possible above --> just from head
0
 

Author Comment

by:Ron_de_Weijze
Comment Utility
Then I get I/O error 32.

So, according to Delphi Help it is a sharing violation and the earlier comments from colleagues about closing the file were possibly right. However when I put close and open statements right in front of AssignFile, the same error occurs with the open statement.

procedure TNotePool.Save(FileName : string);
var
   DataFile   : TextFile;
   i          : integer;
begin
   CloseFile(DataFile);
   OpenFile(DataFile); // <----------------------------------jump to except I/O error 32
   AssignFile(DataFile, FileName);
   Rewrite(DataFile);
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 90 total points
Comment Utility
error 32 --> ERROR_SHARING_VIOLATION

well, the file is in use.
the filehandle/instance must close the file,
therefore this

begin
   CloseFile(DataFile);
   OpenFile(DataFile);

has no effect

seems you have somewhere forgotten to close the file,
or another app or instance keeps this file open.
also possible would be that are rights missed.

meikl ;-)
0
 

Author Comment

by:Ron_de_Weijze
Comment Utility
Haven't solved it yet but you all helped me where to look and what to look for. Thanks!

Ron dW
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

5 Experts available now in Live!

Get 1:1 Help Now