create a file with user defined name

Hi all
I want to know that in C++ is there any process for create a file with user defined name
For example :
User give me a name "abc". Now I need to made a text file with name "abc.txt" and also want to save that file in my computer so that i can store data in that text file.
If there has any process for that please tell me what it is.
Last edited on
If entered name is of type string, just use + to concatenate ".txt" to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>

int main()
{
	std::string name;

	std::cout << "Enter name: ";
	std::getline(std::cin, name);

	name += ".txt";

	std::cout << "File name is: " << name << '\n';
}

bro i don't need to show the file name only i also need to create file with that name and save that file in my computer
Is there any process for that??
Last edited on
ofstream myfile(mypathandfilename); //eg "c:\folder\file.dat"
i also need to create file with that name and save that file in my computer
Is there any process for that??


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

int main()
{
	std::string name;

	std::cout << "Enter name: ";
	std::getline(std::cin, name);

	name += ".txt";

	std::ofstream ofs(name);

	if (!ofs.is_open()) {
		std::cout << "Cannot open file for output\n";
		return 1;
	}

	// output data to ofs stream here as needed

	ofs.close();
}



Thanks a lot brother
It's work .
And this the procedure I want
thanks a lot again bro
Topic archived. No new replies allowed.