Thread: Problem with writing to files

  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
    2,158
    Maybe you forgot to include this line?
    Code:
    #include <fstream>
    Post what the errors say, help you will be alot easier then.

  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
    28,413
    std::ostream, not ostream.
    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

  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
    28,413
    Post your updated code with the exact error messages.
    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

  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
    28,413
    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.
    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

  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
    28,413
    Oh, because now you wrote std::ostream instead of std::ofstream. My mistake from an earlier post, actually.
    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

  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, 01:30 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 06: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, 06:03 AM