How do make a file with a custom name?

Jul 31, 2012 at 3:08am
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();
}
Jul 31, 2012 at 3:52am
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());
Jul 31, 2012 at 4:13am
@IceThatJaw,

I'm sorry to ask this, but isn't supposed to be .c_str()?
Jul 31, 2012 at 7:36am
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.
Jul 31, 2012 at 7:46am
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.
Jul 31, 2012 at 8:02am
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");
Jul 31, 2012 at 8:08am

@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 Jul 31, 2012 at 8:09am
Jul 31, 2012 at 8:13am

@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 Jul 31, 2012 at 8:14am
Aug 1, 2012 at 12:28am
Right, it's because open is a member of ofstream and not string.
Topic archived. No new replies allowed.