Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am working on a project and I am trying to create a folder and a file inside of the folder but it doesn't work

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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”);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading