how could I creat a file at desired folder?

Below is my code and what I want to do is
1)I open the "newfile1.txt"and read the first line(e.g Cat) from this file
2) and then create the Cat.txt at "C:\user\user\Desktop\history"

Currently, I could creat Cat.txt at the folder where my program run but I want to creat file @ "C:\user\user\Desktop\history" instead.

{

ifstream readfile("newfile1.txt);

string str;
getline(readfile,str);
str=str+".txt";
ofstream outfile(str.c_str());
readfile.seekg(iso_base::beg);
outfile<<readfile.rdbuf();

readfile.close();
outfile.close();

}

Recommended Answers

All 2 Replies

So just add the full path to the filename.

str = "C:\\user\\user\\Desktop\\history\\" + str;
str += ".txt";

So just add the full path to the filename.

str = "C:\\user\\user\\Desktop\\history\\" + str;
str += ".txt";

thanks,Ancient Dragon.

sorry one more question.

When I create Cat.txt, there is one extra file(which has no file name) has been created too. What did i do wrong in my code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.