简体   繁体   中英

Error when changing location of ofstream file C++

I had code that looked like this:

std::vector <std::string> info;
info.push_back("10");
info.push_back("this is a line to print");
std::ofstream myfile;
myfile.open("list.txt");
myfile<<"first*"<<info[0]<<"\n";

which worked fine but when, it creates the file and prints the information, but when I changed it to this:

std::vector <std::string> info;
info.push_back("10");
info.push_back("this is a line to print");
std::ofstream myfile;
myfile.open("Output\\list.txt");
myfile<<"first*"<<info[0]<<"\n";

it creates the file but the instant I try to output to the file the whole thing crashes. I am using code::blocks with a GNU gcc compiler on windows 8.

provide the absolute path as "C:\\Documents\\yourprojects\\yourcurrentproject\\Output\\list.txt". and it will work fine. i tried it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM