//Using ofstream to write to a file
#include<fstream>
#include<iostream>
#include<string>
int main(){
usingnamespace std ;
//Getting filename
cout <<"Enter the name of the file ";
string filename ;
cin >> filename ;
//creating the file
ofstream outf(filename); //error
if(!outf){
cerr << "File could not be created " << endl;
}
//Getting filedata
cout << "Enter the data to be written to the file";
char data[100]
cin.getline(data,100);//only writes the first word of line
//Writing data to the file
outf << "This is the start of the file " << endl;
outf << data << endl ;
outf << "This is the end of the file " << endl;
return 0;
}
If the change the filename to char * filename , it gives runtime error ie the program simply crashes.