From: bukinm AT inp DOT nsk DOT su (Michael Bukin) Newsgroups: comp.os.msdos.djgpp Subject: Re: Desperately need help with "fopen" Date: 10 Apr 1997 04:34:54 GMT Organization: BINP SD RAS Lines: 51 Message-ID: <5ihqle$48j@sky.inp.nsk.su> References: <334B568B DOT DBF AT ife DOT ee DOT ethz DOT ch> Reply-To: bukinm AT inp DOT nsk DOT su NNTP-Posting-Host: h-bukin.inp.nsk.su To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp >fopen("test.txt", "r") returns NULL as long as I have this file >("test.txt") opened with MS Word for Windows. Other programs (emacs, >editor, notepad and DOS' type) have no problem reading the file!!! As >soon as I close the file (it's no longer opened with Word) then >fopen("test.txt", "r") works fine! > >It seems there is some file-locking (sharing) mechanism with Win95 but >shouldn't it be possible to open the file for reading????????? The following works on Win95 (using explicit sharing): #include int main (int _argc, char* _argv[]) { char* filename; char buffer[1024]; int handle; FILE* file; filename = (_argc > 1) ? _argv[1] : "test.txt"; if (_dos_open (filename, O_RDONLY | SH_DENYNO, &handle)) { perror (filename); return 1; } file = fdopen (handle, "r"); if (file == 0) { perror (filename); return 1; } /* Use `file' here. */ if ((fgets (buffer, sizeof (buffer), file) == 0) && ferror (file)) { perror (filename); return 1; } fprintf (stderr, "%s\n", buffer); if (fclose (file)) { perror (filename); return 1; } return 0; } BTW, Emacs for dos can't open files already opened in Word (at least, not on my machine).