create a file with user defined name

Oct 26, 2020 at 4:47pm
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 Oct 26, 2020 at 5:04pm
Oct 26, 2020 at 5:01pm
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';
}

Oct 26, 2020 at 5:03pm
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 Oct 26, 2020 at 5:05pm
Oct 26, 2020 at 5:07pm
ofstream myfile(mypathandfilename); //eg "c:\folder\file.dat"
Oct 26, 2020 at 5:18pm
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();
}



Oct 26, 2020 at 5:30pm
Thanks a lot brother
It's work .
And this the procedure I want
thanks a lot again bro
Topic archived. No new replies allowed.