Hiya Michael!
How about workarounds? Executing a file copy then reading that temp file? Anything else you can think of?
Thanks!
Main Topics
Browse All TopicsI'm using the following function to mimic the tail command in unix. It needs to open a file and read from it. As you can see I'm only opening it in readonly mode but I still get the error that I cannot access the file. Can anyone suggest an alternate method to accomplish this task?
function tform1.Tail(FileName:strin
var
S: TStream;
C: Char;
L: Integer;
begin
S := TFileStream.Create(FileNam
try
S.Seek(Position, soBeginning);
L := S.Size-Position;
SetLength(Result, L);
S.Read(Result[1], L);
finally
S.Free;
end;
end;
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
When using a shell copy function I can indeed copy the file, though my own internal functions seem to be keeping ahold of the file - I'm sure I can chase that down though. Here is my copy file function using shellapi
function scShellCopyFile(FormHandle
var
F : TShFileOpStruct;
begin
F.Wnd:=FormHandle;
F.wFunc:=FO_COPY;
F.pFrom:=PChar(StrFrom+#0)
F.pTo:=PChar(StrTo+#0);
F.fFlags := FOF_ALLOWUNDO or FOF_RENAMEONCOLLISION;
if BlnSilent then
F.fFlags := F.fFlags or FOF_SILENT;
if ShFileOperation(F) <> 0 then
result:=False
else
result:=True;
end;
I found that I had an open filemap handle on the file and killed it - this let go of my file so I could mess about with the temp file. I suppose the only question would be should the shell be able to copy a file yet delphi not be able to read it (readonly). I have my solution to a point - it will work though it's a little roundabout. Any other ideas do you think?
ok, didn't see your last comment before my last post
so it should be possible to read from the file
this kind of streamcreation i never used (seems to be a overloaded method)
S := TFileStream.Create(FileNam
maybe it makes a differentce if u use
S := TFileStream.Create(FileNam
a copy and then a read makes not much sense, if the file is huge,
but of course on smaller files it may be a workaround
meikl ;-)
Alright,
When using your OR operator for that open action, I do not get the error that the file is in use at all. My biggest problem seems to be in this function now. It is supposed to open the file and count the number of characters in it. The files I'm dealing with are fairly small - under 10mb in general - and when run on an unlocked file it takes less than 2 seconds.
When running this function on my locked file, it returns -1 instead of the correct number of characters.
Any idea why? I believe it's related to the question's original issue
note that in the following function I've also tried the flag FILE_FLAG_RANDOM_ACCESS in the createfile.
function GetFileLengthInCharacters(
var
filesize:integer;
FileHandle:tHandle;
CharCounter:integer;
begin
FileHandle := CreateFile(pansichar(FileN
CharCounter := GetFileSize(FileHandle, nil);
fileclose(filehandle);
result := charcounter;
end;
Perhaps I'm amiss here in that I misunderstood something that another programmer did because of his var names. As I look now it does look like this is a roundabout way of doing it. How would you rewrite the function to return the filesize? What do you think the best way would be.
Remember that the output of this function should return a value that would be appropriate to the input of the tail function in my first post.
thank you.
Yikes,
No luck. Here's the status.
if I copy off a backup of the file, get the length, then use that for the input in the tail function, it DOES return data properly. This implies that the filestream that tail uses is good and it IS reading the file properly using that method.
The first method I used returns properly on the backup (unlocked) file and -1 on the locked file.
The findfirst method you pasted returns 0. I modified it to return the actual return value (checking for a -1 or other errorcode but it's actually returning 0. This would imply that it's not finding the file, I would assume. However, i've been using the same var and value for the file as in the tail function so I can assume that it is indeed valid. A caveat here is that the file I'm working with is on a UNC path i.e. \\domain\server\folder\fil
I did not find anything that states that UNC paths are not valid when using findfirst.
Any more ideas? I CAN go back to copying to a temp file but it's obviously not the ideal solution.
Back.
Sorry for the delay!
I never did get it to read from the file. This is odd for a couple reasons:
windows can copy it without unlocking it
text editors can read from it without unlocking it
findfirst is still returning 0 (file not found) though the same var, which contains the path to the filename, can be used to copy the file with no issues. the path is correct but findfirst just isn't working.
I'm about ready to give up and go with the file backup / read / etc method. It's just frustrating because I know the file CAN be read - text editors are doing it. I would just like to know why delphi isnt.
Ho hum
No dice.
I still get the following error when trying the loadfromfile
First chance exception at $7C812A5B. Exception class EFOpenError with message 'Cannot open file "\\server\file.txt". The process cannot access the file because it is being used by another process'. Process Project1.exe (2312)
I will repeat for clarity that I can, every time, open the file in notepad, notepad++, word and copy it through explorer
This is perplexing isn't it?
Last (for today at least)
I wrote up a function in C++ that I would use to read the filesize but it ALSO returns -1:
void __fastcall TForm1::Button1Click(TObje
{
FILE *fp;
int letter;
if((fp = fopen("\\server\file.txt",
{
DWORD dwSizeLo, dwSizeHi;
dwSizeLo = GetFileSize ( fp, &dwSizeHi);
char buffer[35];
String mysize = ltoa(dwSizeLo, buffer, 10);
lbl->Caption = mysize;
}
fclose(fp);
}
No luck with this either. This is really perplexing!
Business Accounts
Answer for Membership
by: kretzschmarPosted on 2006-11-29 at 09:22:03ID: 18038651
it could be that another process did open this file exclusive
-> no chance to read it by your process