c++ - File output error -
my program crashes after user inputs outputfilename. c1
char array, out
string , len
int length of c1.
here code:
ofstream outfile; char outputfilename[256]; cout << "enter output file name: "; cin >> outputfilename; cout << endl; outfile.open(outputfilename,ios::trunc); for(int i=0; i<len-1; i++){ //-1 b/c added \n @ end. out[i] = c1[i]; } outfile << out; outfile.close();
i think i'd more this:
{ std::cout << "please enter output file name: "; std::string outputfilename; std::getline(outputfilename, std::cin); std::ofstream outputfile(outputfilename.c_str()); outputfile.write((static_cast<char *>(&c1), len); } // file closes automatically when goes out of scope.
Comments
Post a Comment