Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

exception on Rewrite(DataFile)

Posted on 2004-03-30
15
Medium Priority
?
1,267 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
ID: 10715585
?? 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
ID: 10715709
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
ID: 10715936
the file may be still opened by yourself,
did you close the file?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ron_de_Weijze
ID: 10716015
Yes I did.
0
 
LVL 11

Assisted Solution

by:shaneholmes
shaneholmes earned 240 total points
ID: 10716600
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 240 total points
ID: 10718230
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
ID: 10719028
Thank you for your comments and suggestions. I will try them out and let you know asap.
0
 

Author Comment

by:Ron_de_Weijze
ID: 10722290
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
ID: 10722342
what for an exception do you get??
0
 

Author Comment

by:Ron_de_Weijze
ID: 10722526
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
ID: 10722619
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
ID: 10722624
btw. typos possible above --> just from head
0
 

Author Comment

by:Ron_de_Weijze
ID: 10722735
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 270 total points
ID: 10722922
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
ID: 10727291
Haven't solved it yet but you all helped me where to look and what to look for. Thanks!

Ron dW
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

773 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