How do make a file with a custom name?

I want to be able to make a name of the file for my zip to cbr converter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string Dname="";

    cout << "Converter\n" << endl;

    cout << "Name of the file:";
    cin >> Dname;

    ofstream name;
    name.open (Dname".zip");

    cin.get();
}
name.open (Dname".zip");

This is no good. Ofsteam needs a cstring. It can either be in the full form "some_string" or it might be a std::string with a .c_std() method.

Like:
string std;

name.open(std.c_std());
@IceThatJaw,

I'm sorry to ask this, but isn't supposed to be .c_str()?
Um how do i make a zip extension? Also when i copy and paste to the console window it competently ignores the spaces and everything after it.Please help.
After you've read in the name of the file, you can use std::string's append() method to add ".zip". Then send Dname.c_str() to the filestream.
If you don't want to change Dname you can do
name.open((Dname + ".zip").c_str());

If you are using C++11 it's enough to do
name.open(Dname + ".zip");

@IceThatJaw,

I'm sorry to ask this, but isn't supposed to be .c_str()?


Don't ask this, just correct me and move on.
I was close enough, right? haha
Last edited on

@Peter87

C:\Users\Ultimax\Desktop\Cbr convert\CBR Convert.cpp||In function `int main()':|
C:\Users\Ultimax\Desktop\Cbr convert\CBR Convert.cpp|18|error: 'struct std::string' has no member named 'open'|
||=== Build finished: 1 errors, 0 warnings ===|
Last edited on
Right, it's because open is a member of ofstream and not string.
Topic archived. No new replies allowed.