Here is my code, how do i create a file in the sub directory contacts? Every time the file is created, it appears in the same directory as my program.
int main(){
ofstream myfile("\\contacts");
myfile.open ("a");
myfile.close();
}
Specify the full path in the constructor:
ofstream myfile(".\\contacts\\a"); // or just "contacts/a"
if (myfile.is_open())
{
}
The posted code attempts to create a file called "\\contacts"
and then another file called "a"
.
Note:
ofstream
will not create intermediate directories: "contacts"
must exist prior to the use of the ofstream
.ofstream
so it is unnecessary to explicitly call myfile.close()
.