Mail Archives: djgpp/1997/04/10/08:22:24
>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 <some headers>
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).
- Raw text -