visual studio - C++ will not create a file to output text to? [SOLVED] -


so, have here simple snippet of c++ code merely askes filepath, checks if filepath exists, , if does, program closes. if doesn't, creates file , writes text it. path enter c:\temp.txt, no matter path use result same: no file created. using visual studio 2010.

string outfile = ""; cout << "enter file path output file not exist already: "; cin >> outfile; ofstream file_writer; file_writer.open(outfile, ios_base::out | ios_base::in); if (!file_writer.fail()) {     cout << "this file exists already!" << endl;     cout << strerror(errno) << endl;     system("pause");     return 255; } file_writer << "hello!";  file_writer.close(); 

you try:

string outfile = ""; cout << "enter file path output file not exist already: "; cin >> outfile; ofstream file_writer; file_writer.open(outfile, ios::in); if (file_writer.good()) { //this checks if file exists     cout << "this file exists already!" << endl;     system("pause");     return 255; } file_writer.open(outfile, ios::out); //now since know file doesn't exist, can create safely if (!file_writer.good()) {     cout << "failed create file!" << endl;     return 254; } file_writer << "hello"; file_writer.close(); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -