Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. !file trickiness

!file trickiness

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I have the following file code:

    #include
    #include
    using namespace std;

    int main() {

    //add code below this line
    
    string path = "student/text/readpractice.txt";
    
    try {
        ifstream file;
        file.open(path); //content of file goes into memory buffer
        if (!file) {
            throw runtime\_error("File failed to open.");
        }
        cout << file.rdbuf(); //read the buffered content
        file.close();
    }
    
    catch (exception& e) {
        cerr << e.what() << endl;
    }
    
    //add code above this line
    
    return 0;
    

    }

    Can someone help me understand how this line works:

    if (!file) {

    How can you use the exclamation point operator on what is probably a class? Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded. Any ideas? Thanks.

    M L 2 Replies Last reply
    0
    • M mike7411

      I have the following file code:

      #include
      #include
      using namespace std;

      int main() {

      //add code below this line
      
      string path = "student/text/readpractice.txt";
      
      try {
          ifstream file;
          file.open(path); //content of file goes into memory buffer
          if (!file) {
              throw runtime\_error("File failed to open.");
          }
          cout << file.rdbuf(); //read the buffered content
          file.close();
      }
      
      catch (exception& e) {
          cerr << e.what() << endl;
      }
      
      //add code above this line
      
      return 0;
      

      }

      Can someone help me understand how this line works:

      if (!file) {

      How can you use the exclamation point operator on what is probably a class? Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded. Any ideas? Thanks.

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      There is an operator! in ifstream [std::basic_ios<CharT,Traits>::operator! - cppreference.com](https://en.cppreference.com/w/cpp/io/basic\_ios/operator!) [https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open\](https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open)

      CI/CD = Continuous Impediment/Continuous Despair

      M 1 Reply Last reply
      0
      • M Maximilien

        There is an operator! in ifstream [std::basic_ios<CharT,Traits>::operator! - cppreference.com](https://en.cppreference.com/w/cpp/io/basic\_ios/operator!) [https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open\](https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open)

        CI/CD = Continuous Impediment/Continuous Despair

        M Offline
        M Offline
        mike7411
        wrote on last edited by
        #3

        Interesting. I tried doing a "Step Into" on that line in the debugger, but it won't go to it. If I do a "Go to Definition", it goes to this in xiosbase:

        \_NODISCARD bool \_\_CLR\_OR\_THIS\_CALL operator!() const noexcept /\* strengthened \*/ {
            return fail();
        }
        

        However, I put a breakpoint there, and it doesn't break.

        1 Reply Last reply
        0
        • M mike7411

          I have the following file code:

          #include
          #include
          using namespace std;

          int main() {

          //add code below this line
          
          string path = "student/text/readpractice.txt";
          
          try {
              ifstream file;
              file.open(path); //content of file goes into memory buffer
              if (!file) {
                  throw runtime\_error("File failed to open.");
              }
              cout << file.rdbuf(); //read the buffered content
              file.close();
          }
          
          catch (exception& e) {
              cerr << e.what() << endl;
          }
          
          //add code above this line
          
          return 0;
          

          }

          Can someone help me understand how this line works:

          if (!file) {

          How can you use the exclamation point operator on what is probably a class? Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded. Any ideas? Thanks.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          mike7411 wrote:

          Can someone help me understand how this line works:

          the statement:

          if (!file)

          means if the variable file equals null then ... But you have already instantiated file with the line:

          ifstream file;

          So it is very unlikely that file will be null. The open method sets the failbit status if the open fails, and that is what you need to test. If you use STL classes then you must follow the STL rules, not the old C-style ones. See std::basic_ifstream<CharT,Traits>::open - cppreference.com[^] for full details.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups