using << operator

This is a discussion on using << operator within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a pointer to an ofstream object, and I am trying to use it to write to a ...

  1. #1
    Registered User
    Join Date
    Nov 2005

    using << operator


    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:

    // 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??

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

    Try using

    So you're writing to the adress of the pointer rather then the pointer object?

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

  3. #3
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    I would think that the file is not closed properly. Did you delete logfile ?


  4. #4
    Registered User
    Join Date
    Nov 2005


    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
    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!

    "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
    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, 08:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03: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, 06:23 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21