Problem with writing to files

This is a discussion on Problem with writing to files within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to make a function where I can input a filename and a string for insertion into ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69

    Problem with writing to files

    Hello,
    I am trying to make a function where I can input a filename and a string for insertion into a file.
    Code:
    void write(string filename, string text)
    {
        ofstream file;
        file.open(filename);
        file << text;
        file.close();
        return;
    }
    Can anyone tell me what is wrong with this? It gives me an error on the 3rd and 4th lines when I try to compile.
    Thanks,
    Adam

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,689
    Maybe you forgot to include this line?
    Code:
    #include <fstream>
    Post what the errors say, help you will be alot easier then.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    No, I have fstream
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <fstream>
    
    using std::endl;
    using std::string;
    
    //***WRITES*FILES*********
    void write(string filename, string text)
    {
        ofstream file(filename);
        file << text;
        file.close();
        return;
    }
    Dev C++ gives these error messages
    line 12 - '
    line 12 - [Each
    line 12 - parse
    line 12 - `file'
    Last edited by beanroaster; 12-22-2007 at 11:49 PM. Reason: Forgot to add something
    Adam

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    std::ostream, not ostream.
    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

  5. #5
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Ok, now for line 12 its giving me error no
    Its also giving me some new errors in iosfwd and fstream when I used std:: ostream.
    Adam

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Post your updated code with the exact error messages.
    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

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <ctime>
    
    using std::endl;
    using std::string;
    
    //***WRITES*FILES*********
    void write(string filename, string text)
    {
        std::ostream file(filename);
        file << text;
        file.close();
        return;
    }
    line 12 - no
    (iosfwd) line 61 - candidates
    are - std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const
    line 14 - no
    Adam

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Ah. That's because the constructor that you want for an output stream take a null terminated C-style string as an argument. As such, you should write:
    Code:
    void write(const std::string& filename, const std::string& text)
    {
        std::ostream file(filename.c_str());
        file << text;
        file.close();
        return;
    }
    The file.close() and return are actually unnecessary.
    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

  9. #9
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Its still giving me the same error messages.
    Adam

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Oh, because now you wrote std::ostream instead of std::ofstream. My mistake from an earlier post, actually.
    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

  11. #11
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Haha, it works!
    Thanks for all the help and sorry for being such a noob.
    Adam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 02:30 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 07:00 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Problem with enum and files
    By Robert_Sitter in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2005, 01:40 AM
  5. Problem splitting program into different files
    By kzar in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 07:03 AM

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