file opening errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan

    file opening errors

    How can I get detailed information on WHY an error occurs upon
    attempt to open? I know how to use the member function ".good()" but
    cannot find how to get information on why it failed to open.

  • Ney André de Mello Zunino

    #2
    Re: file opening errors

    Alan wrote:
    [color=blue]
    > How can I get detailed information on WHY an error occurs upon
    > attempt to open? I know how to use the member function ".good()" but
    > cannot find how to get information on why it failed to open.[/color]

    AFAICT, the standard language will only allow you to test whether the
    stream is ready/good to be used or not. "Detailed information", such as
    whether the file operation failed because of lack of permission, etc.
    would have to be obtained from the OS, with OS-specific calls. Then
    again, I could be missing something.

    Regards,

    --
    Ney André de Mello Zunino

    Comment

    • Alan

      #3
      Re: file opening errors

      Here is more about what is happending. The following code produces an
      error on a file "test1.txt" , the second time through the "for" loop.
      The errors printed out are:

      File failure
      Reached end of file

      Any ideas? Thanks, Alan

      Code:

      opts.filter = "test*.txt" ;
      FindFile find(opts);
      find.search();
      nfiles = (int) find.filelist.s ize();

      for (int i = 0; i < nfiles; i++)
      {
      string fullname = FindFile::combi nePath(find.fil elist[i].path,
      find.filelist[i].fileinfo.cFile Name);
      infile.open(ful lname.c_str());

      if (infile.good()& &outfile.good() )
      while (getline(infile ,in_string,'\n' ))
      {
      . . .
      }
      else
      {
      if (!infile.good() ) cout << "*** Unable to read input
      file ***\n";
      else cout << "*** Unable to read output file ***\n";
      if (infile.bad()) cout << "Reading error\n";
      if (infile.fail()) cout << "File failure\n";
      if (infile.eof()) cout << "Reached end of file\n";
      }
      infile.close();
      }

      Comment

      • Panjandrum

        #4
        Re: file opening errors

        Alan wrote:[color=blue]
        > How can I get detailed information on WHY an error occurs upon
        > attempt to open? I know how to use the member function ".good()" but
        > cannot find how to get information on why it failed to open.[/color]

        If you used fopen ...

        perror ();
        strerror (errno);

        Comment

        • Larry I Smith

          #5
          Re: file opening errors

          Alan wrote:[color=blue]
          > Here is more about what is happending. The following code produces an
          > error on a file "test1.txt" , the second time through the "for" loop.
          > The errors printed out are:
          >
          > File failure
          > Reached end of file
          >
          > Any ideas? Thanks, Alan
          >
          > Code:
          >
          > opts.filter = "test*.txt" ;
          > FindFile find(opts);
          > find.search();
          > nfiles = (int) find.filelist.s ize();
          >
          > for (int i = 0; i < nfiles; i++)
          > {
          > string fullname = FindFile::combi nePath(find.fil elist[i].path,
          > find.filelist[i].fileinfo.cFile Name);
          > infile.open(ful lname.c_str());
          >
          > if (infile.good()& &outfile.good() )
          > while (getline(infile ,in_string,'\n' ))
          > {
          > . . .
          > }
          > else
          > {
          > if (!infile.good() ) cout << "*** Unable to read input
          > file ***\n";
          > else cout << "*** Unable to read output file ***\n";
          > if (infile.bad()) cout << "Reading error\n";
          > if (infile.fail()) cout << "File failure\n";
          > if (infile.eof()) cout << "Reached end of file\n";
          > }
          > infile.close();[/color]


          infile.clear(); // clear EOF & error flags
          [color=blue]
          > }
          >[/color]

          Comment

          • Alan

            #6
            Re: file opening errors

            Larry: Thanks. This did it. Alan
            [color=blue]
            > infile.clear(); // clear EOF & error flags
            >[/color]

            Comment

            • Jack Klein

              #7
              Re: file opening errors

              On 19 Jun 2005 14:13:56 -0700, "Panjandrum " <pan6c7yc0j@spa mbob.com>
              wrote in comp.lang.c++:
              [color=blue]
              > Alan wrote:[color=green]
              > > How can I get detailed information on WHY an error occurs upon
              > > attempt to open? I know how to use the member function ".good()" but
              > > cannot find how to get information on why it failed to open.[/color]
              >
              > If you used fopen ...
              >
              > perror ();
              > strerror (errno);[/color]

              Neither the C nor C++ standards require that the standard library
              function fopen() set errno on a failure. Your implementation might do
              so, but that is a non-standard extension and code using it will not be
              portable.

              --
              Jack Klein
              Home: http://JK-Technology.Com
              FAQs for
              comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
              comp.lang.c++ http://www.parashift.com/c++-faq-lite/
              alt.comp.lang.l earn.c-c++

              Comment

              • Panjandrum

                #8
                Re: file opening errors

                Jack Klein wrote:[color=blue]
                > "Panjandrum " wrote in comp.lang.c++:[color=green]
                > > If you used fopen ...
                > >
                > > perror ();
                > > strerror (errno);[/color]
                >
                > Neither the C nor C++ standards require that the standard library
                > function fopen() set errno on a failure. Your implementation might do
                > so, but that is a non-standard extension and code using it will not be
                > portable.[/color]

                You portably get the last system error message. Nothing more, nothing
                less.

                Comment

                Working...