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:
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:
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;
}