Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
06-27-2004, 11:11 PM
|
#1
|
Member
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223
Rep:
|
create a directory in C++
hello:
I'm using ofstream to create a file and write on it, however I'd like also to create a directory and there to put the file.... will ofstream create the dir automatically if I do:
ofstream file("c:/dir/file.dat", ios :: out);
thanks for your help!!!
Poeta
Last edited by poeta_boy; 06-27-2004 at 11:32 PM.
|
|
|
06-28-2004, 10:50 AM
|
#2
|
LQ Newbie
Registered: May 2004
Posts: 28
Rep:
|
It's really not a good solution, but you can make use of the system statement...
Code:
system("mkdir <path>\<dir>");
|
|
|
06-28-2004, 10:57 AM
|
#3
|
LQ Newbie
Registered: Aug 2003
Location: Montreal, Canada
Distribution: NetBSD
Posts: 12
Rep:
|
You can use mkdir(2)
Code:
int
mkdir(const char *path, mode_t mode);
|
|
|
06-29-2004, 04:02 PM
|
#4
|
Member
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223
Original Poster
Rep:
|
the mkdir prototipe is included in which library?
thanks
|
|
|
06-29-2004, 04:23 PM
|
#5
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
#include <sys/stat.h>
#include <sys/types.h>
do man 2 mkdir for more infos
[edit]
Forget it, I did not see you program in windows platform
Last edited by keefaz; 06-29-2004 at 04:24 PM.
|
|
|
06-29-2004, 10:07 PM
|
#6
|
Member
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223
Original Poster
Rep:
|
thanks a lot, but I do need it for windows 
|
|
|
07-01-2004, 12:43 AM
|
#7
|
Member
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223
Original Poster
Rep:
|
I've managed to do it... or almost:
I have this string:
string path; // have to convert it to char* so it's usable
char *temp = new char[path.length());
for(int i = 0; i < path.length(); i++)
temp[i] = path[i];
now, after including direct.h
_mkdir(temp);
and that would be it.... however, lets say the original string contains:
path = "C:/file";
after the convertion to char* I get:
C:/fil═²²²²
why is that happening? the new dir C:/fil═²²²² does appear....
Thanks a lot!
(My post number 100th!!!)
|
|
|
07-02-2004, 07:15 AM
|
#8
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
Did you try :
_mkdir( path.c_str() );
?
|
|
|
07-02-2004, 09:10 AM
|
#9
|
Member
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223
Original Poster
Rep:
|
thanks a lot, actually I had to copy each char with a for loop, but now I added the 0 char at the end and it worked just fine.. I didn't know of that
c_str() thing but I'll use now
Thanks!
|
|
|
11-02-2011, 12:32 PM
|
#10
|
Member
Registered: Oct 2002
Posts: 569
Rep:
|
Is it really that difficult to make a directory with c++ in Linux? After reading all this I still don't have a clue and I can't get anything to work.
I'm using g++ 3.4.6. If I want to create a directory in the local run directory call 'buzz' how do I do it without using a system call?
|
|
|
11-02-2011, 12:56 PM
|
#11
|
Member
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379
|
Quote:
Originally Posted by sharky
Is it really that difficult to make a directory with c++ in Linux? After reading all this I still don't have a clue and I can't get anything to work.
I'm using g++ 3.4.6. If I want to create a directory in the local run directory call 'buzz' how do I do it without using a system call?
|
Please refrain from using necromancy on the forums. This thread is 7 years old.
C++ does not have function for making a directory, because "directory" is a feature of operating system, and C++ is cross-platform. Function for making a directory is normally provided by operating system.
In case of linux the function could be easily found if you actually tried to read the manual.
|
|
|
11-02-2011, 01:04 PM
|
#12
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,988
|
Quote:
Originally Posted by sharky
Is it really that difficult to make a directory with c++ in Linux? After reading all this I still don't have a clue and I can't get anything to work.
|
Re-read it again, you will find these:
man 2 mkdir
mkdir ("buzz", 0755);
|
|
|
11-02-2011, 01:24 PM
|
#13
|
Member
Registered: Oct 2002
Posts: 569
Rep:
|
Quote:
Originally Posted by SigTerm
Please refrain from using necromancy on the forums. This thread is 7 years old.
C++ does not have function for making a directory, because "directory" is a feature of operating system, and C++ is cross-platform. Function for making a directory is normally provided by operating system.
In case of linux the function could be easily found if you actually tried to read the manual.
|
It came up on a search. Thanks for the help.
|
|
|
11-02-2011, 01:28 PM
|
#14
|
Member
Registered: Oct 2002
Posts: 569
Rep:
|
This is what worked for me.
Quote:
string PROJ;
getline (cin, PROJ);
const char * cPROJ = PROJ.c_str();
mkdir(cPROJ,0750);
|
The key was converting the string to char.
This also worked.
Quote:
string PROJ;
getline (cin, PROJ);
mkdir(PROJ.c_str(),0750);
|
Sorry for the "necromancy" but why does it matter?
|
|
|
11-02-2011, 01:30 PM
|
#15
|
Member
Registered: Oct 2002
Posts: 569
Rep:
|
Quote:
Originally Posted by NevemTeve
Re-read it again, you will find these:
man 2 mkdir
mkdir ("buzz", 0755);
|
Thanks,
|
|
|
All times are GMT -5. The time now is 11:52 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|