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. Question concerning IO file

Question concerning IO file

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 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
    tpndtbk
    wrote on last edited by
    #1

    Hi all, I want to create and read/write a file. If I use fstream class, I can open only the file already exits. If I want to create a new file. What can I do? Thank you very much. Tran Phuong Nga

    T 1 Reply Last reply
    0
    • T tpndtbk

      Hi all, I want to create and read/write a file. If I use fstream class, I can open only the file already exits. If I want to create a new file. What can I do? Thank you very much. Tran Phuong Nga

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      tpndtbk wrote: If I use fstream class, I can open only the file already exits. If I want to create a new file. What can I do? use ios::in in Second Parameter of fstream object initialization ----------------------------- "I Think this Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk

      T 1 Reply Last reply
      0
      • T ThatsAlok

        tpndtbk wrote: If I use fstream class, I can open only the file already exits. If I want to create a new file. What can I do? use ios::in in Second Parameter of fstream object initialization ----------------------------- "I Think this Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk

        T Offline
        T Offline
        tpndtbk
        wrote on last edited by
        #3

        HI, I did like following: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); //Names is a struct and name is a variable of Names type fout->close(); ----------- So what is wrong in these codes? I run this code, no errors but the file is not created! Thank you

        D R 2 Replies Last reply
        0
        • T tpndtbk

          HI, I did like following: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); //Names is a struct and name is a variable of Names type fout->close(); ----------- So what is wrong in these codes? I run this code, no errors but the file is not created! Thank you

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

          I tried your code (except I did not use a pointer) and it worked as expected. Perhaps it is a permission-related issue.


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          1 Reply Last reply
          0
          • T tpndtbk

            HI, I did like following: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); //Names is a struct and name is a variable of Names type fout->close(); ----------- So what is wrong in these codes? I run this code, no errors but the file is not created! Thank you

            R Offline
            R Offline
            RChin
            wrote on last edited by
            #5

            As with David's comment above, but also be aware of the location that your file is being created. tpndtbk wrote: fout = new fstream("data.dat", ios::in | ios::out | ios::binary); You supplied only the filename, not a definite path, which means that the file will be created in the current directory. This, in most cases, may very well be in your ..\debug\ subfolder.


            I Dream of Absolute Zero

            T 2 Replies Last reply
            0
            • R RChin

              As with David's comment above, but also be aware of the location that your file is being created. tpndtbk wrote: fout = new fstream("data.dat", ios::in | ios::out | ios::binary); You supplied only the filename, not a definite path, which means that the file will be created in the current directory. This, in most cases, may very well be in your ..\debug\ subfolder.


              I Dream of Absolute Zero

              T Offline
              T Offline
              tpndtbk
              wrote on last edited by
              #6

              HI, THank you. My program runs without any error but there's no file created. I think I have problem with writing permission. But I don't know how to solve it. Can anyone help me out?

              1 Reply Last reply
              0
              • R RChin

                As with David's comment above, but also be aware of the location that your file is being created. tpndtbk wrote: fout = new fstream("data.dat", ios::in | ios::out | ios::binary); You supplied only the filename, not a definite path, which means that the file will be created in the current directory. This, in most cases, may very well be in your ..\debug\ subfolder.


                I Dream of Absolute Zero

                T Offline
                T Offline
                tpndtbk
                wrote on last edited by
                #7

                And there's a strange thing that: If I write: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); if (!fout->is_open()) { cout << " can't open the file" << endl; return 1; } fout->write((const char*)name, sizeof(Names)); ---> the result is "can't open the file"; But if I don't check if the file is open. fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); --> there's no error but there's no file created. I think if the file doesn't exist, the instruction "fout->write((const char*)name, sizeof(Names));" should cause an error. Right?

                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