exception on Rewrite(DataFile)

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
Ron_de_WeijzeAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kretzschmarConnect With a Mentor Commented:
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
 
kretzschmarCommented:
?? 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
 
Ron_de_WeijzeAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
kretzschmarCommented:
the file may be still opened by yourself,
did you close the file?
0
 
Ron_de_WeijzeAuthor Commented:
Yes I did.
0
 
shaneholmesConnect With a Mentor Commented:
maybe close the file

and process all the messages and then attempt to rewrite

Application.ProcessMessages

Just thinking out load

shane
0
 
calinutzConnect With a Mentor Commented:
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
 
Ron_de_WeijzeAuthor Commented:
Thank you for your comments and suggestions. I will try them out and let you know asap.
0
 
Ron_de_WeijzeAuthor Commented:
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
 
kretzschmarCommented:
what for an exception do you get??
0
 
Ron_de_WeijzeAuthor Commented:
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
 
kretzschmarCommented:
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
 
kretzschmarCommented:
btw. typos possible above --> just from head
0
 
Ron_de_WeijzeAuthor Commented:
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
 
Ron_de_WeijzeAuthor Commented:
Haven't solved it yet but you all helped me where to look and what to look for. Thanks!

Ron dW
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.