variable in a file name

Hi :)
I have a problem, well it's not a problem, I just don't know how to do it :/
so, here we go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
using namespace std;

int main () {
cout << "Name:";
cin >> name;

	ofstream file;
		file.open ("name.filename", ios::in | ios::ate);
			file << "whatever...";
			file.close();
		cout << "file was created!\n";

  return 0;
}

I want to name my "filename" file like name.filename. According to what you enter as "name". Got it? :))
Help me :)
closed account (4z0M4iN6)
Do you think, it could be a good idea to make also a variable for the file name?
Last edited on
closed account (zb0S216C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string Input;
    std::cin >> Input;

    std::string Final("Name." + Input);
    std::ofstream OutFile(Final.c_str());

    if(OutFile.is_open())
    {
        // Something...
        OutFile.close();
        return(0);
    }

    std::cout << "ERROR: Failed to create the file\n";
    return(1);
}

I'm sure you can figure out what this code does.

Wazzak
Awesome! thx :))))
working nice :)
closed account (4z0M4iN6)
@Framework

Can you think, why I didn't give justinko the code?
Dadabe, you're undoubtedly the most patronising ass on these boards.
closed account (4z0M4iN6)
iHutch105 wrote:


Dadabe, you're undoubtedly the most patronising ass on these boards.

If you would have asked me this, I could have answered you.
Topic archived. No new replies allowed.