954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

create, write & read file to/from folder

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?

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

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");
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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..

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

how to create from input?

string folder;
getline(cin, folder);

mkdir(folder); ???

cannot compile!

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

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);
 }


}

Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 

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:

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

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?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

how to check the folder is exist or not?

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 
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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You