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
    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
    ZuK
    ZuK is offline
    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,893
    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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

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