ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i'm wanting to see if a file is currently opened by my program.
if it is i just want to read from it, if not open it then read from it.
example:
fstream f;
if(!f.open(filename))
{
f.open(filename);
if(!f){ printf("File not there");}
}
f.getline(stuff,256);
is there any way to see if the file is open? cause what i have as the example
is what i have in my prog so far. i know that by putting "if(!filename)" will
only check to see if the file exists but NOT see if it is currently open. i've
also tried just "if(!f)" as the initial if statement but it did not read from
the file at all.
ok, when you declare your fstream set it equal to NULL. Then later you can just say:
if (f == NULL)
{
f.open(filename);
if(!f)
{
printf("File not there");
}
}
As you seam to already know, if fopen fails it will return null, so if you initialize your fstream variable to null then when you go to test it the value should either still be null or it should be pointing to the file stream.
Oh... perhaps there is another way you can interpret your question too... do a man errno to see what value fopen sets it to when the file is already in use...... You can then test errno for that value if you get a null return from fopen.
Something like
if(!(f=fopen(filename))
{
if(errno == ETXTBSY)
printf("File is busy.\n");
}
not shure what you mean by setting your fstream f to null. do you mean "fstream f = NULL;" ? cause that doesnt work for me.
aslo i get a bunch of errors from using fopen. is there a header file for it?
Last edited by chewysplace; 02-20-2004 at 07:54 PM.
I am sorry, I am an idiot. I read your f.open as f.fopen apparently, that and I completely missed the C++ part of this... what I gave you is some good old C code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.