-
help with fstream
I am very new to c++ and need some help with fstream.h
Could someone please tell me how to ask the user for a name and make that the name of a .txt please? I imagine it would be something like this...
int num;
cin>>num;
ofstream a_file(num, ".txt");
etc...
If someone could help with this, or direct me to a advanced tutorial, it would be most appreciated.
-
Code:
#include <fstream>
#include <iostream>
#include <string>
int main(){
std::ofstream out;
std::string str;
std::cout << "Enter the name of the file" << std::endl;
std::cin >> str;
str.append(".txt");
out.open(str.c_str(),std::ios::out);
if(!out.is_open()){
std::cout << "Could not open" << std::endl;
return 1;
}
std::cout << "Success!!! Created " << str << std::endl;
out << "This is text";
}