CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Question writing to screen AND disk:

    Hello:
    I am researching the code to write to both screen and to hard drive (file) simultaneously, if this is possible; I am aware that I can program cin to write to file on disk, but it will not appear on the screen (?). If I am taking input from a user and I wish to copy it, why can I not write to disk while it is printed to console before them also? I am unsure about this and I hope for some knowledgable insight into this operation.

    Thank-you in advance for your assistance,
    Matthew Mansoor
    "Nay, you love this present life!"

    Save your keystrokes!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: writing to screen AND disk:

    Open an ifstream to the file you want to write to and output both to the screen and file.
    Code:
    #include <fstream>
    #include <string>
    using namespace std;
    ...
    ifstream ifs;
    ifs.open("somefile.txt", ios_base::out);
    if (ifs.is_open())
    {
      string str;
      // Get input from user in str
      
      // Now, output to screen
      cout << str;
      // and output to file
      ifs << str;
    
      ...
    
      ifs.close();
    }
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Smile Re: writing to screen AND disk:

    Marc:
    Cool Thank-you very much. I will try this today. Excellent.


    Matthew Mansoor
    "Nay, you love this present life!"

    Save your keystrokes!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured