Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checking if file is open

Posted on 1998-08-18
18
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

721 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