Thread: ofstream error

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    196

    Thumbs up ofstream error

    I keep getting below ofstream error.
    [Error] no matching function for call to 'std::basic_ofstream<char>:pen(std:fstream&, std::string&)'

    What could be wrong here? Is it compiler issue? How to fix it?

    Below is my code.


    Code:
    ofstream outputFile;
    ofstream fs;
    // create a name for the file output
    std::string filename = "test.csv";
    
    /int i;
    
    
    int main(int argc, char** argv)
    {
        // create and open the .csv file
        fs.open(outputFile,filename);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    std::basic_fstream<CharT,Traits>::open - cppreference.com
    You need at least C++11 to open a file with a filename stored in a std::string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    196
    I think my compiler support c++11.I checked the Compiler settings and it shows "Language standard (-std) ISO C++ 11. Is that what you meant?

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    196
    should i change code this way?

    Code:
    ofstream outputFile;
    //ofstream fs;
    // create a name for the file output
    std::string filename = "test.csv";
    std::ofstream fs;
    
     
     
    int main(int argc, char** argv)
    {
            fs.open(filename);

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    Yes, but you have another issue: the parameters are wrong. It should have been:
    Code:
    // create a name for the file output
    const std::string filename = "test.csv";
    
    int main(int argc, char** argv)
    {
        // create and open the .csv file
        ofstream fs(filename);
    Notice that I declared fs to be a local variable, and initialised it at the same time, i.e., declare variables near first use in as local a scope as feasible. I let filename remain at file scope, but changed it to be const: this way it is merely a constant rather than adding to global state.
    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

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    196
    With above changes, output file (test.csv) doesn't even get created.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    196
    Hi laserlight

    can you help me suggest a working code?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    I literally posted code.
    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
    Nov 2008
    Posts
    196
    I still got the same ofstream error at this line --> fs.open( outputFile, filename);

    Code:
    ofstream outputFile;
    //ofstream fs;
    // create a name for the file output
    const std::string filename = "test.csv";
    std::ofstream fs;
     
      
      
    int main(int argc, char** argv)
    {
            fs.open( outputFile, filename);

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    You have two ofstream objects: outputFile and fs. You only have one output file. Pick one. That is, if you want to use outputFile, then delete fs. If you want to use fs, then delete outputFile. What you're doing right now is opening fs and then using outputFile. Of course that doesn't work. It is like opening a bank account at Bank A and another account at Bank B, depositing all your money into the account at Bank A, then asking why your account at Bank B is empty.

    The other thing: don't use global variables. Force yourself to use local variables, even when global variables seem far more convenient. This will develop your discipline to think in terms of local state, which in turn will allow you to write larger and larger programs without these programs becoming very difficult to maintain.
    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. Using ofstream
    By aaleclaire in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2012, 10:25 PM
  2. ofstream
    By Ducky in forum C++ Programming
    Replies: 54
    Last Post: 12-29-2007, 01:47 PM
  3. Using ofstream
    By alvifarooq in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2004, 09:06 PM
  4. If/Ofstream
    By SirCrono6 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2003, 04:58 AM
  5. ifstream/ofstream
    By unanimous in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2001, 02:33 PM

Tags for this Thread