?
Solved

Delphi :: File Write Exception

Posted on 2007-10-11
8
Medium Priority
?
5,759 Views
Last Modified: 2008-01-09
Dear Experts,

I have a function in Delphi 7 which does some file writing on a text file.
If executing the function for the first time then it works fine but when I the file
already has data I receive a exception. "I/O error 32"
Please have a look at my function and tell me where am I going wrong?

function TfrmMain.InsertParameter(const Val1, Val2, FixedAmount, Percentage :Double;
      Cond :String; CondAmount :Double): Integer;
var
  fParam :TextFile;
  NewID :Integer;
begin
  Try
    Try
      NewID := GetID;
      AssignFile(fParam, sParamFile);
      If FileExists(sParamFile) then Append(fParam)
      Else Rewrite(fParam);

      Writeln(fParam, NewID, Val1, Val2, FixedAmount, Percentage, Cond, CondAmount);
    Finally
      CloseFile(fParam);
    end;
    Result := NewID;
  Except
    Result := -1;
  end;
end;

Thanks!
0
Comment
Question by:Marius0188
8 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20058930
Hmm... I think you might have to call  reset///

[Delphi] procedure Reset(var F: file); overload;
[Delphi] procedure Reset(var F: file; RecSize: Integer); overload;
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20059012
Cancel that - I'm just looking into it again, Reset opens file at beginning, Append opens file at end, and Rewrite creates new file at end. So far I can't see any problems.. I'll keep checking.
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 20059041
From windows documentation I/O error #32 is:

The process cannot access the file because it is being used by another process.

Are you sure there is no other process using this file (or your own application)?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Expert Comment

by:Johnjces
ID: 20059063
I ran your code on XP SP2 as an admin  and had no problems at all. Wrote to c:\test.txt

I/O error 32 means "SHARING VIOLATION", in other words: you're trying to access a file which is in use by some other process (in your program or "locked" by another application).

Maybe something has it open elsewhere or the system hasn't had time to close the file before you try and write to it again.

You might do an "is file in use function" and check to see if it is closed before you try to write to it.

function IsFileInUse(FileName: TFileName): Boolean;
var
  HFileRes: HFILE;
begin
  Result := False;
  if not FileExists(FileName) then Exit;
  HFileRes := CreateFile(PChar(FileName),
                         GENERIC_READ or GENERIC_WRITE,
                         0,
                         nil,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);
  Result := (HFileRes = INVALID_HANDLE_VALUE);
  if not Result then
    CloseHandle(HFileRes);
end;
 
 
{Example:}
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsFileInUse('c:\windows\notepad.exe') then
    ShowMessage('Notepad is in use.');
  else
    ShowMessage('Notepad not in use.');
end;

Might help. But your code worked great for me. I just had to rem NewID := GetID.

John

0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20059093
One think to check:
Make sure sParamFile variable is not empty and points to an actual filename (that doesn't exst). sParamFile must not be null.
0
 

Author Comment

by:Marius0188
ID: 20059469
Ok, looks like it was some sharing violation.

Just a quick question:

Can I write double and string values in one "WriteLn()"?
And also can I read it back with "ReadLn"?
Because I am getting an "invalid numeric input" error when I have a string value amongst the double

Thanks again!
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20059527
you can write them:

var s: string;
 d: double;
 f: textfile;
begin
 ...
 WriteLn(f, s + FloatToStr(d));
end;

you cannot read them with ReadLn though.

what kind of file are you trying to write?
0
 
LVL 17

Expert Comment

by:mokule
ID: 20063742
I think You can write and read in a single command.
Do it like this.

procedure TForm1.Button1Click(Sender: TObject);
var
  F: TextFile;
  a: double;
  t:string;
begin
  AssignFile(F,'c:\test.txt');
  Rewrite(F);
  a := 1.2334;
  t:= 'egerg f';
  Writeln(F,a,' ',t);
  CloseFile(F);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  F: TextFile;
  a: double;
  c: Char;
  t:string;
begin
  AssignFile(F,'c:\test.txt');
  Reset(F);
  Readln(F,a,c,t);
  CloseFile(F);
  Memo1.Lines.Add(FloatToStr(a));
  Memo1.Lines.Add(t);
end;

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

840 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