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?

Member Avatar for iamthwee

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 iamthwee

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.

@n3st3d_l00p
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 iamthwee

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.