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. ofstream driving me nuts

ofstream driving me nuts

Scheduled Pinned Locked Moved C / C++ / MFC
iostutorial
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.
  • B Offline
    B Offline
    Brian van der Beek
    wrote on last edited by
    #1

    Hi, I have a class that needs to output data to a file. for this I defined a std::ofstream as a class member. However for some reasons I cannot get this to work. Writing to file works fine when I define a std::ofstream locally in a function, but for some reason it does not work when I use the std::ofstream as a class member. This is driving me nuts. example: #include <fstream> class MyClass { public: MyClass() {}; virtuel ~MyClass(){}; void WriteSomething(); private: std::ofstream m_file; } void MyClass::WriteSomething() { CString str = "test\n"; std::ofstream file; // does not work. m_file.open("c:\\test.txt", std::ios::out); m_file.write(str, str.Getlength()); m_file.close() // does work. file.open("c:\\test2.txt", std::ios::out); file.write(str, str.Getlength()); file.close() } -- modified at 17:00 Thursday 12th January, 2006

    C 1 Reply Last reply
    0
    • B Brian van der Beek

      Hi, I have a class that needs to output data to a file. for this I defined a std::ofstream as a class member. However for some reasons I cannot get this to work. Writing to file works fine when I define a std::ofstream locally in a function, but for some reason it does not work when I use the std::ofstream as a class member. This is driving me nuts. example: #include <fstream> class MyClass { public: MyClass() {}; virtuel ~MyClass(){}; void WriteSomething(); private: std::ofstream m_file; } void MyClass::WriteSomething() { CString str = "test\n"; std::ofstream file; // does not work. m_file.open("c:\\test.txt", std::ios::out); m_file.write(str, str.Getlength()); m_file.close() // does work. file.open("c:\\test2.txt", std::ios::out); file.write(str, str.Getlength()); file.close() } -- modified at 17:00 Thursday 12th January, 2006

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      When you say 'does not work', what does it do ? Does this only get called once ? Does any other code use the member variable ? Christian Graus - Microsoft MVP - C++

      B 1 Reply Last reply
      0
      • C Christian Graus

        When you say 'does not work', what does it do ? Does this only get called once ? Does any other code use the member variable ? Christian Graus - Microsoft MVP - C++

        B Offline
        B Offline
        Brian van der Beek
        wrote on last edited by
        #3

        The destination file is created, but remains empty. I have to say, I gave up and now use a CFile, which does use, but I still like to know why the ofstream does not work.

        _ 1 Reply Last reply
        0
        • B Brian van der Beek

          The destination file is created, but remains empty. I have to say, I gave up and now use a CFile, which does use, but I still like to know why the ofstream does not work.

          _ Offline
          _ Offline
          __yb
          wrote on last edited by
          #4

          Thats probably because of caching: The function's local stream is closed as the function exists so the file is flushed to disk and closed. On the other hand, the class member stream is not flushed so u c a file but no data. To check this u just have to flush the file explicitly. [I'd paste some code here but I use FILE*fh=fopen or CreateFile. Never c++ streams nor CFile]

          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