LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2004, 07:01 PM   #1
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
checking file state in C++


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.
 
Old 02-20-2004, 07:28 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 02-20-2004, 07:31 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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");
}

Last edited by jtshaw; 02-20-2004 at 07:32 PM.
 
Old 02-20-2004, 07:53 PM   #4
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Original Poster
Rep: Reputation: 30
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.
 
Old 02-20-2004, 08:00 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.

Sorry.....
 
Old 02-20-2004, 08:27 PM   #6
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Original Poster
Rep: Reputation: 30
i'm just gona make a separate function to put all the info into a linked list
and access it from the list.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't edit state file. jpc1258 Linux - Newbie 2 08-24-2004 03:30 PM
Checking if file exists Haiyadragon Programming 2 04-26-2004 10:07 AM
file properties state where x should says s ? wolfe2554 Linux - General 1 04-17-2004 08:57 AM
Checking a file for numbers chrisk5527 Linux - General 1 09-12-2003 08:50 AM
Checking if a file exists in C? tristan_vdv Programming 3 05-24-2002 10:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration