Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

determining why a file cannot be written


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.

Ben wrote:
> ofstream filestream;
> filestream.open(path);

> if(!filestream.is_open())
> {

      std::cerr << "Could not open ";
      std::perror(path); // needs #include <cstdio>

<Ben> wrote in message...
> Hello, I hope this is the correct group for this posting :-)

Yup! <G>

std::ofstream 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::ostream &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;
     }

--
Bob R
POVrookie

On Jun 6, 3:29 pm, Ben wrote:

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

--
James Kanze (GABI Software)             email:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

BobR wrote:
> 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?
Thanks everybody. I have taken all of your suggestions into consideration :-)

Ben

Juha Nieminen wrote in message...
> BobR wrote:
> > 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?

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.

--
Bob R
POVrookie

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc