944,073 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 35244
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 23rd, 2006
0

create, write & read file to/from folder

Expand Post »
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.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 23rd, 2006
0

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

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:

C++ Syntax (Toggle Plain Text)
  1. #include <direct.h>
  2.  
  3. int main()
  4. {
  5. mkdir("c:/myfolder");
  6. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 23rd, 2006
0

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

To create new file in folder do this:-

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 23rd, 2006
0

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

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

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

how to create from input?

string folder;
getline(cin, folder);

mkdir(folder); ???

cannot compile!
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 23rd, 2006
0

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

how to create from input?

string folder;
getline(cin, folder);

mkdir(folder); ???

cannot compile!
Through Command Line
C++ Syntax (Toggle Plain Text)
  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. }
}
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 23rd, 2006
0

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

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.
Moderator
Reputation Points: 3281
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Sep 23rd, 2006
2

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

@n3st3d_l00p
If you want a portable solution then you can use Boost Filesystem library. There's a create_directory function for creating directories.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 23rd, 2006
0

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

Click to Expand / Collapse  Quote originally posted by Grunt ...
Through Command Line
C++ Syntax (Toggle Plain Text)
  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:
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 23rd, 2006
1

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

How about working with const char arrays instead?

C++ Syntax (Toggle Plain Text)
  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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: VC++2005 express help
Next Thread in C++ Forum Timeline: need debugging help.. i've look at this too long





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC