How can I use a string as a file name?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GA17
    New Member
    • Apr 2012
    • 1

    #1

    How can I use a string as a file name?

    I am trying to create a file with variable name. In order to do that I use a string, I let the user scan for it, but the problem appears when I try to "fopen", since I am not sure for the syntax in this case.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    fopen needs a C-style string: char array with a \0 terminator.

    Use the c_str() method of string:

    Code:
    string name("myfile.txt");
    fopen(name.c_str());

    Comment

    Working...