Menu

ostream problem

SagaEterna
2003-01-01
2012-09-26
  • SagaEterna

    SagaEterna - 2003-01-01

    i try to create a simple ostream by using:

    ofstream Log("Log.txt", ios::out | ios::app);

    with namespace std define and iostream include: but i have the following error:

    variable `std::ofstream Log' has initializer but incomplete type

    Could someone help me?

    Thanks

     
    • Nobody/Anonymous

      you do NOT need the ios::out and ios::app when creating an ofstream. This is handled by the standard creator in the ofstream.h file

       
    • Nobody/Anonymous

      #include <fstream>
      #include <stdlib.h>
      using namespace std;
      int main()
      {
      ofstream Log("D:\\Log.txt", ios::out | ios::app);
      system("pause");
      return 0;
      }

      The code above works perfectly on my computer. I have a question for you Stephane - did you try to write data at the end of the file Log.txt? (Is that the reason for ios::app?). If so - I tried the following and it works:

      ofstream Log("D:\\Log.txt", ios::ate);
      Log << "Hi!";

      You don't need ios::out because this if ofstream (which means output file stream). If you run this program several times, you'll see that Hi! is written at the end of the file every time.
      Franjo

       
    • Nobody/Anonymous

      Here is a simple code:

      #include <iostream>
      #include <fstream>

          

      int main( )
        {
        std::ofstream writeToFile;   
      writeToFile.open("isFile.txt");
      writeToFile << "    ---------------Just a little program---------------" << std::endl;
      writeToFile << " A most simple program" << std::endl;
      writeToFile << "Do you like it ?";
      writeToFile.close();
       
           system("PAUSE");
          return 0;
        }

      N@N!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.