Why does this not work, and are there other alternatives?
This will eventually be used in a loop of sorts.. the different fileName's will be determined by the program in real time..

#include <fstream>
#include <iostream>

int main (int argc, char** argv)
{
    std::string fileName; 
    fileName = "testFile.txt"; // passed to program at runtime
    
    std::ofstream output;
    output.open(fileName); // path of ofstream = string fileName
    output << "success";
    output.close();
    
    return 0;
}

:?:

Recommended Answers

Here is the correction output.open(fileName.c_str());

Jump to Post

All 3 Replies

Here is the correction output.open(fileName.c_str());

aah cheers, I was wondering what that was for in that snippet..
I've got a lot to learn..

> Why does this not work,
...and next time describing the problem in detail along with error messages would be recommended rather than the so famous 'this does not work' statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.