determining why a file cannot be written

B

Ben

Hello, I hope this is the correct group for this posting :)

For the following kind of code fragment:

ofstream filestream;
filestream.open(path);

if(!filestream.is_open())
{
cerr<< ......

}


I am finding that occasionally a file will not be written.

My question is, is there a way of finding out exactly why the file
cannot be written? (E.g. some kind of returned error code to determine
whether it is an issue with permissions or file space etc).

Thanks,
Ben.
 
B

BobR

Hello, I hope this is the correct group for this posting :)

Yup! said:
For the following kind of code fragment:

ofstream filestream;
filestream.open(path);

if(!filestream.is_open())
{
cerr<< ......
}

I am finding that occasionally a file will not be written.

My question is, is there a way of finding out exactly why the file
cannot be written? (E.g. some kind of returned error code to determine
whether it is an issue with permissions or file space etc).
Thanks, Ben.


std::eek:fstream file( path );

if( not file.is_open() ){
std::cout<<" file error="<<file.flags()
<<"\n ios::good="<<file.good()
<<"\n ios::bad="<<file.bad()
<<"\n ios::eof="<<file.eof()
<<"\n ios::fail="<<file.fail()<<std::endl;
}
file.clear();

Beyond that, you'll need to go to the OS's file system(I assume there is
one. <G>).

[ not standard. GNU GCC (MinGW) ] (from an old post)

// -------------------------------------------------------
// Many times, it's not just important to determind if the file exist, but
// it's also important to determind if it's a file or directory.
// I did not save the posters name, so, I can not credit him/her.
// - answer -
// http://www.codecomments.com/archive323-2005-10-664071.html
// seems to say that it was Axter.
// -------------------------------------------------------

#include <sys/stat.h> // plus other headers here

bool FileExist(char const *FileName){
struct stat my_stat;
return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(char const *FileName){
struct stat my_stat;
if(stat(FileName, &my_stat) != 0) return false;
return ((my_stat.st_mode & S_IFDIR) != 0);
}

// int main(){
void FileExistMain(std::eek:stream &cout){
bool v1 = FileExist("c:/autoexec.bat");
bool v2 = FileExist("c:/nofile.bat");
bool v3 = FileExist("c:/config.sys");
bool v4 = FileExist("c:/nofile2.bat");
bool v5 = IsDirectory("c:/windows");
bool v6 = IsDirectory("c:/notA_dir");
bool v7 = IsDirectory("c:/WINNT");

cout<<"v1 ="<< std::boolalpha<<v1<<std::endl;
cout<<"v2 ="<< std::boolalpha<<v2<<std::endl;
// ....
cout<<"v7 ="<< std::boolalpha<<v7<<std::endl;
return;
}
 
J

James Kanze

Hello, I hope this is the correct group for this posting :)

Sort of:).
For the following kind of code fragment:
ofstream filestream;
filestream.open(path);
if(!filestream.is_open())
{
cerr<< ......
}
I am finding that occasionally a file will not be written.
My question is, is there a way of finding out exactly why the file
cannot be written? (E.g. some kind of returned error code to determine
whether it is an issue with permissions or file space etc).

Not really, at least not officially. The error reporting in
iostream is minimal, to say the least.

In practice, on most systems, if you'll read errno immediately
after the operation, you should get some indication, but exactly
what will depend on the system. Try something like:

if ( ! filestream.is_open() ) {
std::cerr << strerror( errno ) << std::endl ;
}

and see what that gives you.
 
J

Juha Nieminen

BobR said:
Beyond that, you'll need to go to the OS's file system(I assume there is
one. <G>).

I have been using perror() (and in some cases strerror()) to print
these types of error messages without problems in several systems with
several compilers. What's wrong with it?
 
B

Ben Jones

Thanks everybody. I have taken all of your suggestions into consideration :)

Ben
 
B

BobR

Juha Nieminen wrote in message...
I have been using perror() (and in some cases strerror()) to print
these types of error messages without problems in several systems with
several compilers. What's wrong with it?

Uh.... Oh, I know. It's outside the '(std::*)stream' definitions? <G>
It's 'C' code? <G>

No, I see no problem. The more tools you have, the easier the job.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top