How can you tell if a file is already open in C

I am programming with an old Borland Turbo C++. The program runs in DOS mode ( non protected mode ). My program opens a file and displays its contents. I use fopen() to open the file. When I had the file already open with another program ( a hex editor ), and I tried to open the file in my program, my program just crashed really bad and I had to ctr-alt-del to shut it down. I thought if the file was already open fopen would detect it and return a null?? How can I be sure a file is not already open?

Also one more question. What is a good Turbo C compatable way to tell what drives are connected to the computer, for instance floppy drives / hard drives / cd drives etc? Currently I use chdrive or something to try and switch to the drive and if I can switch to it my program assumes I have that drive hooked up. But there is a problem, I have no floppy drives hooked up but my program seems to think I have both A: and B: drives hooked up, and when I click on those it locks the program up. Any help would be appreciated thanks.

Comments

  • : I am programming with an old Borland Turbo C++. The program runs in DOS mode ( non protected mode ). My program opens a file and displays its contents. I use fopen() to open the file. When I had the file already open with another program ( a hex editor ), and I tried to open the file in my program, my program just crashed really bad and I had to ctr-alt-del to shut it down. I thought if the file was already open fopen would detect it and return a null?? How can I be sure a file is not already open?

    [blue]I don't know of any way to tell if another process has opened the file if it did not open it in one of the sharing modes that prevents other processes from accessing it. If you wrote both processes you could either use sharing functions such as fsopen() or lock it with a mutex.[/blue]

    :
    : Also one more question. What is a good Turbo C compatable way to tell what drives are connected to the computer, for instance floppy drives / hard drives / cd drives etc? Currently I use chdrive or something to try and switch to the drive and if I can switch to it my program assumes I have that drive hooked up. But there is a problem, I have no floppy drives hooked up but my program seems to think I have both A: and B: drives hooked up, and when I click on those it locks the program up. Any help would be appreciated thanks.
    :
    [blue]I have no idea[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion