hello.. help me solve this :cheesy:


when a new car or motorcycle created, the system should create a folder with the name of the car or motorcycle. the hierarchy should be like this:

Vehicle(main exe folder)
+Car
-Porsche
-Ferrari
+Motorcycle
-Suzuki
-Honda

new vehicle info's folder shoud be create as example
---> c:\vehicle\motorcycle\suzuki125\
---> c:\vehicle\car\ferrari\ferrariSE\


so, from C++
- how to create new folder?
- how to create new file in new folder?
- how to write/read file from the folder?

Dani AI

Generated

Quick, practical update that builds on the examples here. Using system("mkdir") or compiler-specific mkdir (as shown by and ) works but is non-portable and can be unsafe. correctly pointed to Boost.Filesystem for portability; today the standard, portable option is std::filesystem (C++17+). Use it to create nested folders, check existence, and then create/read files with normal streams.

#include <filesystem>
#include <fstream>
#include <iostream>
#include <system_error>

namespace fs = std::filesystem;

int main() {
    std::string type = "motorcycle", model = "suzuki125"; // or read with getline
    fs::path dir = fs::path("C:/vehicle") / type / model;

    std::error_code ec;
    bool created = fs::create_directories(dir, ec);
    if (ec) {
        std::cerr << "Failed creating " << dir << ": " << ec.message() << '\n';
        return 1;
    }

    fs::path file = dir / "info.txt";
    std::ofstream ofs(file.string());
    if (!ofs) { std::cerr << "Cannot create " << file << '\n'; return 1; }
    ofs << "type=" << type << '\n' << "model=" << model << '\n';
    ofs.close();

    std::ifstream ifs(file.string());
    for (std::string line; std::getline(ifs, line); )
        std::cout << line << '\n';
}

Notes and tips: use fs::exists(dir) / fs::is_directory(dir) to test existence; prefer the std::error_code overload of create_directories if you want to avoid exceptions. Read user input with std::getline and build the fs::path (do basic sanitizing to avoid absolute-path or “..” injection). Avoid system() calls for security and portability. If stuck on an older compiler, use Boost.Filesystem as suggested. Finally, watch permissions and Windows path quirks (escape backslashes in literals or use forward slashes or raw strings).

Recommended Answers

All 12 Replies

Member Avatar for Member #46692

so, from C++
- how to create new folder?
- how to create new file in new folder?
- how to write/read file from the folder?


To create a folder do this:

#include <direct.h>

int main()
{
  mkdir("c:/myfolder");
}
Member Avatar for Member #46692

To create new file in folder do this:-

#include <direct.h>
#include <fstream>

using namespace std;
int main()
{
  mkdir("c:/myfolder");
  ofstream write ("c:/myfolder/myfile.txt");
  
  write << "Sup mo fo";
  
  write.close();
}

And so on.

thank you very well..

i have done the same coding.. but use system("mkdir folder") instead of mkdir("folder");

maybe i just forget to include direct.h before this?

urmm..

how to create from input?

string folder;
getline(cin, folder);

mkdir(folder); ???

cannot compile!

how to create from input?

string folder;
getline(cin, folder);

mkdir(folder); ???

cannot compile!

Through Command Line

#include <iostream>
#include <direct.h>

int main(int argc, char** argv) {
if (argc < 2) {
       std::cerr << "Usage: " << argv[0] << " [new dir name]\n";
       return(EXIT_FAILURE);
 }
if (mkdir(argv[1]) == -1) { // Create the directory
       std::cerr << "Error: " << strerror(errno);
       return(EXIT_FAILURE);
 }

}

Since mkdir() is not a standard C/C++ function, it's not surprising it doesn't work. The standard language is designed to be ignorant of the 'outside world' (directories, screen, keyboard) so anything that uses them outside the rudimentary I/O is compiler dependant. You'll have to look through your compiler documentation to see what functions it provides.


If you want a portable solution then you can use Boost Filesystem library. There's a create_directory function for creating directories.

Through Command Line

#include <iostream>
#include <direct.h>

int main(int argc, char** argv) {
if (argc < 2) {
       std::cerr << "Usage: " << argv[0] << " [new dir name]\n";
       return(EXIT_FAILURE);
 }
if (mkdir(argv[1]) == -1) { // Create the directory
       std::cerr << "Error: " << strerror(errno);
       return(EXIT_FAILURE);
 }

}

how should i use these? run at cmd line? :rolleyes:

Member Avatar for Member #46692

How about working with const char arrays instead?

#include <direct.h>
#include <fstream>
#include <iostream>
#include <cstring>

using namespace std;
int main()
{
  char folder[101];
  cout << "Name your folder mo fo:";
  cin >> folder;
  
  char path[]="c:/";
  
  strcat(path,folder);
 
  mkdir(path);
  
  cout<<"Folder added to c: drive";
  cin.get();
  
}

or look into using c_str() with std::strings?

thank you to all... very useful code for my project! ;)

how to check the folder is exist or not?

how to check the folder is exist or not?

Try creating the folder.
a) If you get an error, it exists or can't be created.
b) If not, it didn't exist but does now.

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.