This is my function:
void Mail::send_mail(int id, std::string send_to, std::string subject, std::string message) {
int receiever_id = find_user(send_to);
std::fstream file("/mails/" + send_to + "/" + std::to_string(accounts.at(receiever_id).number_of_mails) + ".txt", std::ios::out);
email *mail = new email("From: " + accounts.at(id).username, "Subject: " + subject, "Content: " + message);
accounts.at(receiever_id).inbox.push_back(*mail);
file<<"From: "<<accounts.at(id).username<<std::endl;
file<<"Subject: "<<subject<<std::endl;
file<<"Content: "<<message;
accounts.at(receiever_id).number_of_mails++;
delete mail;
rewrite();
return;
}
I want to create a folder mails and a folder with the name of the receiver if it doesn’t exist and then create a txt file inside, but for some reason It doesn’t work.
>Solution :
as @Eugene mentioned in the comments, you cannot create a file inside a directory that doesn’t exist. C++ will not create the directory for you first.
you can use boost to create the directory first, then proceed with your logic.
#include <boost/filesystem.hpp> boost::filesystem::create_directory("dirname");
the reason for using boost is for your code to be cross-platform and work on different operating systems. You can use system() and pass it the command for creating a directory on your OS.
for example, on Linux, this would something like this:
system(“mkdir user1”);