create, write & read file to/from folder

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

create, write & read file to/from folder

 
0
  #1
Sep 23rd, 2006
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?
Last edited by n3st3d_l00p; Sep 23rd, 2006 at 8:17 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: create, write & read file to/from folder

 
0
  #2
Sep 23rd, 2006
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:

  1. #include <direct.h>
  2.  
  3. int main()
  4. {
  5. mkdir("c:/myfolder");
  6. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: create, write & read file to/from folder

 
0
  #3
Sep 23rd, 2006
To create new file in folder do this:-

  1. #include <direct.h>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5. int main()
  6. {
  7. mkdir("c:/myfolder");
  8. ofstream write ("c:/myfolder/myfile.txt");
  9.  
  10. write << "Sup mo fo";
  11.  
  12. write.close();
  13. }

And so on.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

Re: create, write & read file to/from folder

 
0
  #4
Sep 23rd, 2006
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..
Last edited by n3st3d_l00p; Sep 23rd, 2006 at 10:00 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

Re: create, write & read file to/from folder

 
0
  #5
Sep 23rd, 2006
how to create from input?

string folder;
getline(cin, folder);

mkdir(folder); ???

cannot compile!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: create, write & read file to/from folder

 
0
  #6
Sep 23rd, 2006
Originally Posted by n3st3d_l00p View Post
how to create from input?

string folder;
getline(cin, folder);

mkdir(folder); ???

cannot compile!
Through Command Line
  1. #include <iostream>
  2. #include <direct.h>
  3.  
  4. int main(int argc, char** argv) {
  5. if (argc < 2) {
  6. std::cerr << "Usage: " << argv[0] << " [new dir name]\n";
  7. return(EXIT_FAILURE);
  8. }
  9. if (mkdir(argv[1]) == -1) { // Create the directory
  10. std::cerr << "Error: " << strerror(errno);
  11. return(EXIT_FAILURE);
  12. }
}
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: create, write & read file to/from folder

 
0
  #7
Sep 23rd, 2006
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: create, write & read file to/from folder

 
2
  #8
Sep 23rd, 2006
@n3st3d_l00p
If you want a portable solution then you can use Boost Filesystem library. There's a create_directory function for creating directories.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

Re: create, write & read file to/from folder

 
0
  #9
Sep 23rd, 2006
Originally Posted by Grunt View Post
Through Command Line
  1. #include <iostream>
  2. #include <direct.h>
  3.  
  4. int main(int argc, char** argv) {
  5. if (argc < 2) {
  6. std::cerr << "Usage: " << argv[0] << " [new dir name]\n";
  7. return(EXIT_FAILURE);
  8. }
  9. if (mkdir(argv[1]) == -1) { // Create the directory
  10. std::cerr << "Error: " << strerror(errno);
  11. return(EXIT_FAILURE);
  12. }
}

how should i use these? run at cmd line? :rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: create, write & read file to/from folder

 
1
  #10
Sep 23rd, 2006
How about working with const char arrays instead?

  1. #include <direct.h>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7. int main()
  8. {
  9. char folder[101];
  10. cout << "Name your folder mo fo:";
  11. cin >> folder;
  12.  
  13. char path[]="c:/";
  14.  
  15. strcat(path,folder);
  16.  
  17. mkdir(path);
  18.  
  19. cout<<"Folder added to c: drive";
  20. cin.get();
  21.  
  22. }

or look into using c_str() with std::strings?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC