Solved

Checking if file is open

Posted on 1998-08-18
18
215 Views
Last Modified: 2010-04-01
Hi,
I would like to check if a file is allready open for write/read. I know there is a member function of filebuf is_open(). But I don't know how to use it when my file type is ofstream/ifstream.
for example:

ofstream fout ("file.txt");

/* here I want to use is_open() (I don't mean to check if the openning have been succeded, but if the file was openning before) */

fout << "something";

I would like an example.
Thank you
0
Comment
Question by:s_lavie
  • 8
  • 4
  • 2
  • +3
18 Comments
 
LVL 1

Expert Comment

by:payn
ID: 1170658
First of all, is_open works just as well for ofstream and ifstream as for filebuf.

However, that won't help for what you're trying to do. What you really need to do is check if the file's open before opening it. Obviously, once you've opened the file, any test will return positive.

To answer this, I need more information. Are you trying to check whether or not your program already has the file open? Whether or not the current user already has the file open? Whether or not someone, anyone, already has the file open?

Unfortunately, there is no standard, cross-platform way of doing any of these checks. The first you can do yourself by keeping track of every file you open, but even that isn't guaranteed to work (you could open two links to the same file and you wouldn't know they were the same).

To go any farther, you'll have to tell me what OS you're using.

0
 
LVL 23

Expert Comment

by:chensu
ID: 1170659
ofstream has also a member function is_open.

ofstream::is_open
int is_open() const;

Return Value

Returns a nonzero value if this stream is attached to an open disk file identified by a file descriptor; otherwise 0.
0
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1170660
Do you mean open by this or by another program?
Try renaming it to itself, see if that works (won't on NFS/UNIX for example).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170661
If you mean open by another program, do not try the renaming approach.  (It can cause problems as another program could create a new copy of the file and then you can't rename back!)  Instead try opening it in a non-share mode, that is open it with exclusive write and (if desired) exclusive read access.
0
 
LVL 1

Expert Comment

by:payn
ID: 1170662
Actually, nietod's answer makes the most sense, because the only reason I could imagine you'd care if the file's already open is that you want to open it exclusively. In which case, just try to open it exclusively.

But that'll still be just as platform-dependent as anything else. So you still have to tell us what OS and what filesystem you're using.

0
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1170663
I menat rename the file to its original name (i.e. ren("foo", "foo")), so that if it works, nothing changes, and if it does not, you knowsomeone has it open.

Again, very File System dependant.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170664
I see.  But opening the file is really the safest way, especially if you intend to use the file, which would realy be the only reason I can thing of for wanting to know if another program has it locked.  If you tried the rename method you might find it unlocked and then go to open the file and find that another program has locked it in the meantime.

s_lavie, are you there?  Did someone answer your question?  We're trying to help, but we don't even know exactly what you want.  Do you want to see if you have the file open, of if someone else does?
0
 
LVL 4

Expert Comment

by:sganta
ID: 1170665
Hi !

fopen in what mode you want ? if it is read only mode you can directly open the file.
Or else if the file is opened in write mode , then you cannot open the file in write mode.

In that case do the following .

Open the file in the write mode.

If it opens successfully, then you can assume that it is not opened in write mode.
Or else the file is locked by some one else with WRITE option.

Good luck !
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170666
s_lavie, are you there?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Author Comment

by:s_lavie
ID: 1170667
Hi to all,
Sory I was late but I wasn't here.
Anyway, what I meant was that:
Supose I want to write to a file someone has opened, but before I do so, I want to check if it is opened allready, or should I open it myself. The only question is whether or not I can do it and if yes than how???
Thank you all!
0
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1170668
The proper way to do that is to try to open the file in a non-sharing mode.  If the file is already open, you will not be able to open it.  If it isn't open, you will be able to.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170669
For example, you can do

ofstream fout ("file.txt",ios:;sh_none);

if (fout.is_open())
{
   fout << "data";
}
else
{
   cout << "Error: File is in use"
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170670
Was there something you didn't like about the answer?  Something more you wanted?  
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1170671
Hellow nietod!!!

I was abroad for the last two weeks, so I'm sorry I'm answering just now.
When I tried to use:

if(fout.is_open())

I got an error as  is_open() is not a member of ofstream. That it what I don't understand.

Bye Bye
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170672
Well, if a solution isn't working, you should let the expert know rather than just giving a bad grade.  That is in your interest!

However, is_open() is a member of ofstream.  What compler and version are you using?
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1170673
Hi nietod,
Sorry I'm new in this buisness.

I'm using Borland C++ 3.1 (for MS-DOS)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170674
Unfortunately, I don't have BC 3.1 to test with.  It is possible that the is_open() member was not provided with its library, but I doubt it.  (It is now part of the official standard and will definitley be provided in all future releases)

The following code uses is_open() and compiles and works under VC 5.
**************************
#include <fstream.h>

int main()
{
  ifstream ifile;

  if (ifile.is_open())
     cout << "is open";
  else
     cout << "is closed";

 return 0;
}
**************************
I would open the include files for the streams, like fstream.h and the include files it uses and look for a function that might indicate if the file is open.
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1170675
Thank you nietod. you are great!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now