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. stl:fstream exeption-handling and file-closing [modified]

stl:fstream exeption-handling and file-closing [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
5 Posts 4 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.
  • T Offline
    T Offline
    Tomerland
    wrote on last edited by
    #1

    Dear developers, I have the following code:

    try {
    ifstream file;
    file.open("file.txt");

    file >> ... ;	
    file.close();	
    

    }
    catch(......) {
    ...
    ---> do I have to close the file here? <----
    }

    My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?

    modified on Tuesday, May 6, 2008 7:06 AM

    R C T T 4 Replies Last reply
    0
    • T Tomerland

      Dear developers, I have the following code:

      try {
      ifstream file;
      file.open("file.txt");

      file >> ... ;	
      file.close();	
      

      }
      catch(......) {
      ...
      ---> do I have to close the file here? <----
      }

      My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?

      modified on Tuesday, May 6, 2008 7:06 AM

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Tomerland wrote:

      ry { ifstream file; file.open("file.txt"); file >> ... ; file.close(); } catch(...) { ... ---> do I have to close the file here? <---- }

      You won't possibly know the exception that took you into the catch block, since you are catching every possible exception. What if the file wasn't opened and the exception is a FileNotFound one? Dear God, No!

      Tomerland wrote:

      Another question: How can I insert code here with indentation?

      Enclose the code within the <pre> </pre> tags.

      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

      1 Reply Last reply
      0
      • T Tomerland

        Dear developers, I have the following code:

        try {
        ifstream file;
        file.open("file.txt");

        file >> ... ;	
        file.close();	
        

        }
        catch(......) {
        ...
        ---> do I have to close the file here? <----
        }

        My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?

        modified on Tuesday, May 6, 2008 7:06 AM

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Tomerland wrote:

        Or is it automatically closed when the destructor of file runs.

        Why don't you try? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        1 Reply Last reply
        0
        • T Tomerland

          Dear developers, I have the following code:

          try {
          ifstream file;
          file.open("file.txt");

          file >> ... ;	
          file.close();	
          

          }
          catch(......) {
          ...
          ---> do I have to close the file here? <----
          }

          My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?

          modified on Tuesday, May 6, 2008 7:06 AM

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          Tomerland wrote:

          Do I have to close the file in the catch-handler?

          if the file was successfully opened, yes, otherwise, no !

          Tomerland wrote:

          Or is it automatically closed when the destructor of file runs

          nop. and as long as you don't know where the exception were thrown, you can't bother about that...

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • T Tomerland

            Dear developers, I have the following code:

            try {
            ifstream file;
            file.open("file.txt");

            file >> ... ;	
            file.close();	
            

            }
            catch(......) {
            ...
            ---> do I have to close the file here? <----
            }

            My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?

            modified on Tuesday, May 6, 2008 7:06 AM

            T Offline
            T Offline
            Tomerland
            wrote on last edited by
            #5

            Thanks for your replies !!! Another question: What is the code for handling this szenario in the right way? Kind regards

            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