determining why a file cannot be written

Discussion in 'C++' started by Ben, Jun 6, 2007.

  1. Ben

    Ben Guest

    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, Jun 6, 2007
    #1
    1. Advertising

  2. Ben wrote:
    > ofstream filestream;
    > filestream.open(path);
    >
    > if(!filestream.is_open())
    > {

    std::cerr << "Could not open ";
    std::perror(path); // needs #include <cstdio>
    > }
     
    Juha Nieminen, Jun 6, 2007
    #2
    1. Advertising

  3. Ben

    BobR Guest

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


    Yup! <G>

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

    --
    Bob R
    POVrookie
     
    BobR, Jun 6, 2007
    #3
  4. Ben

    James Kanze Guest

    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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 7, 2007
    #4
  5. 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?
     
    Juha Nieminen, Jun 7, 2007
    #5
  6. Ben

    Ben Jones Guest

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

    Ben
     
    Ben Jones, Jun 7, 2007
    #6
  7. Ben

    BobR Guest

    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
     
    BobR, Jun 7, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    571
    reaway lee
    Aug 24, 2003
  2. darrel
    Replies:
    3
    Views:
    574
    Darrel
    Apr 6, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    922
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  5. KaiWen
    Replies:
    102
    Views:
    2,786
    Jorgen Grahn
    Sep 15, 2011
Loading...

Share This Page