Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to open TextFile -ReadOnly with other app running

Status
Not open for further replies.

StuM

MIS
Jan 16, 2001
148
US
I am trying to get read only access to a .CSV file that is currently open with another application that is filling it.

AssignFile(FF, CSVFile);
FileMode:= fmopenread;
Reset(FF);

Error 103 is returned.

If the other application is closed and MY app is started the file opens OK. If I then start the other App, everything works OK until MY app trys to reopen the file.

I have tried different values for "FileMode" for Sharing with no success. The "Other Application" and Excel have the same effect on my program (Error 103).
 
I'm not sure you're doing the file access properly (at least not for the Delphi I'm running - the constant works but I'm not finding it). What I've been doing is

Code:
filemode := 0; {read only}
assign(infile, 'mydata.csv');
reset(infile);

and it works like you're looking for it to work.

But anyway, if the other app expects to have the file in read/write mode, it will generally lock the file to anything but readonly access, and vice versa. For example, if I set filemode to 1, then the file can only be pulled up in read-only mode.

If it helps, you should be able to typecast "infile" with TFileRec, and access "handle" and then double-check what mode you have the file in (fmClosed, fmInput, fmOutput, or fmInOut)
 
I'm using Version 7 of Delphi

Looking at my Delphi help -"Note:To avoid scope conflicts, AssignFile replaces the Assign procedure that was available in early versions of the Delphi product. However, for backward compatibility Assign is still available.

I tried moving the filemode statement before the assign and that didn't help.

FmOpenRead is found in the help under "File open mode constants".

 
You can't always open a file in read-only mode that is open for writing by another program. There's more to it than ReadOnly and ReadWrite access. There's also sharing.

Generally, a program that opens a file in ReadWrite mode also opens it in Exclusive mode - this is the bane of backup programs everywhere.

You need to modify the other app to open the file with sharing enabled, ie. fmReadWrite+fmShareDenyNone or fmReadWrite+fmShareDenyWrite
 
The other program (not written in house) works with Excel opening it for "read-only".... So wouldn't that mean that it "should be" ok for my delphi program to open it ?
 
Stum, try adding fmShareDenyNone as Griffyn suggested:
...
FileMode:= fmopenread + fmShareDenyNone;
...


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I already tried that. Since Excel can open the CSV, after I click on the "Read-Only" message that comes up, doesn't that mean that the "other app" is set-up properly for file sharing, AND I just need to get the correct setting in Delphi,(my program), to open it ?
 
Have you thought about doing something like this:
Code:
  AssignFile(FF, 'mydata.csv');
  {$I-}
  Reset(FF);
  {$I+}
  if IOResult <> 0 then begin
    CopyFile('mydata.csv', 'mydata[1].csv', false);
    AssignFile(FF, 'mydata[1].csv');
    Reset(FF);
  end;
Untested, and there's more you could do to make it idiot proof, but just enough to hopefully give you an idea to work with...

Roo
Delphi Rules!
 
I found the solution. The Filemode DOESN'T WORK with TEXTFILE

VAR
XX,Data1 : String;
TF : TextFile;
begin

XX:='C:\FEEventsM20070919.csv';
AssignFile(TF,XX);
OpenReadText(TF,False);
reset(TF);
Readln(TF,Data1);
Showmessage(Data1);

CloseFile(TF);

Change to:

Var
S: TFileStream;
FNM,Data1 : String;

begin
FNM:='c:\FEEventsM20070919.csv';
S:= TfileStream.Create(FNM,fmOpenRead + fmShareDenyNone );

try
Memo1.Lines.LoadFromStream(S);
Finally
S.Free;
end;
end;

Now I just have to figure out how to load data into Strings, one line at a time.

Thanks for the help.
 
You should be able to copy the TFileStream to a TStringStream
 
You already have, if you have it in a Memo.
Code:
  for i:= 0 to Memo1.Lines.Count -1 do
    showMessage('Line[' + intToStr(i) + '] = ' + Memo1.Lines[i];


Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top