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. ios::in | ios::out error

ios::in | ios::out error

Scheduled Pinned Locked Moved C / C++ / MFC
helpios
10 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.
  • S Offline
    S Offline
    Sujan Dhakal
    wrote on last edited by
    #1

    #include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help

    CPalliniC D X 3 Replies Last reply
    0
    • S Sujan Dhakal

      #include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      As you may check, opening the file with ios::in flag set, fails if the file doesn't exist. :)

      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
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • S Sujan Dhakal

        #include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Member 6910300 wrote:

        I'm not able to write texts in file..please help

        Does the file exist?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Man who follows car will be exhausted." - Confucius

        S 1 Reply Last reply
        0
        • D David Crow

          Member 6910300 wrote:

          I'm not able to write texts in file..please help

          Does the file exist?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          S Offline
          S Offline
          Sujan Dhakal
          wrote on last edited by
          #4

          no the file is expected to be created. in the following case also it outputs "Error" fstream file("c:\\test.txt",ios::out | ios::in); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; }

          D 1 Reply Last reply
          0
          • S Sujan Dhakal

            no the file is expected to be created. in the following case also it outputs "Error" fstream file("c:\\test.txt",ios::out | ios::in); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Then you should use:

            ofstream file("c:\\test.txt", ios::binary);

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Man who follows car will be exhausted." - Confucius

            S 1 Reply Last reply
            0
            • D David Crow

              Then you should use:

              ofstream file("c:\\test.txt", ios::binary);

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              S Offline
              S Offline
              Sujan Dhakal
              wrote on last edited by
              #6

              thanks for the answer but i dont want to use ofstream and ifstream class instead i want to use fstream class only. Is it possible? and i also need the file to be created as using ofstream.

              CPalliniC 1 Reply Last reply
              0
              • S Sujan Dhakal

                #include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help

                X Offline
                X Offline
                xiaozhu8766
                wrote on last edited by
                #7

                maybe your file doesn't exist.check your file. I check your code, is ok. Calvin

                1 Reply Last reply
                0
                • S Sujan Dhakal

                  thanks for the answer but i dont want to use ofstream and ifstream class instead i want to use fstream class only. Is it possible? and i also need the file to be created as using ofstream.

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  if the file does not exist, you have to use

                  fstream file("c:\\test.txt", ios::out | ios::binary);

                  :)

                  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
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  S 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    if the file does not exist, you have to use

                    fstream file("c:\\test.txt", ios::out | ios::binary);

                    :)

                    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
                    [My articles]

                    S Offline
                    S Offline
                    Sujan Dhakal
                    wrote on last edited by
                    #9

                    what's problem with this bit masking ios::in | ios::out | ios::binary since we have specified out in the mask, why the file is not being created. thanks for replying.

                    CPalliniC 1 Reply Last reply
                    0
                    • S Sujan Dhakal

                      what's problem with this bit masking ios::in | ios::out | ios::binary since we have specified out in the mask, why the file is not being created. thanks for replying.

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #10

                      The problem is you can't read from a not exitsting file, I suppose. Your bit mask works if the file do exist. On the other hand, if the file does not exist you should use the

                      ios::out | ios::binary

                      mask. :)

                      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
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      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