Thread: using << operator

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    using << operator

    Hi,

    I have a pointer to an ofstream object, and I am trying to use it to write to a file using the << operator, but it wont work. Here is the code I am using:

    Code:
    // in header file
    
    ofstream* logfile;
    
    // in cpp file
    
    logfile = new ofstream("logfile.txt")
    *logfile << "logging enabled\n";
    But it doesnt work. The file has been created but it wont write anything to it. Is there sonething obvious I am doing wrong??
    Thanks
    Mark

  2. #2
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I'm really bad with pointers, but...

    Try using

    Code:
    &logfile
    So you're writing to the adress of the pointer rather then the pointer object?

    Or try simply,
    Code:
     logfile << ofstream
    Once again, I'm no good shot at pointers, so I may be totaly off-track.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would think that the file is not closed properly. Did you delete logfile ?

    Kurt

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Thanks

    The problem was that the file was not being closed in the program.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Which is why you should allocate such things as files either on the stack, or use a smart pointer for them. (Actually, you should use smart pointers for nearly every dynamic allocation.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you really need a pointer to an ofstream object? You could create an ofstream object, and then use its open() member function at some later point, after all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. overloading the output operator <<
    By neandrake in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2004, 02:25 PM
  4. Returning failure result for operator << istream
    By _Elixia_ in forum C++ Programming
    Replies: 0
    Last Post: 07-05-2003, 05:23 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM