LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-27-2004, 11:11 PM   #1
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Rep: Reputation: 31
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.
 
Old 06-28-2004, 10:50 AM   #2
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Rep: Reputation: 15
It's really not a good solution, but you can make use of the system statement...

Code:
system("mkdir <path>\<dir>");
 
Old 06-28-2004, 10:57 AM   #3
Dergro
LQ Newbie
 
Registered: Aug 2003
Location: Montreal, Canada
Distribution: NetBSD
Posts: 12

Rep: Reputation: 0
You can use mkdir(2)

Code:
int
mkdir(const char *path, mode_t mode);
 
Old 06-29-2004, 04:02 PM   #4
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
the mkdir prototipe is included in which library?

thanks
 
Old 06-29-2004, 04:23 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
#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.
 
Old 06-29-2004, 10:07 PM   #6
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
thanks a lot, but I do need it for windows
 
Old 07-01-2004, 12:43 AM   #7
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
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!!!)
 
Old 07-02-2004, 07:15 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Did you try :
_mkdir( path.c_str() );
?
 
Old 07-02-2004, 09:10 AM   #9
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
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!
 
Old 11-02-2011, 12:32 PM   #10
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
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?
 
Old 11-02-2011, 12:56 PM   #11
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by sharky View Post
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.
 
Old 11-02-2011, 01:04 PM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
Originally Posted by sharky View Post
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);
 
Old 11-02-2011, 01:24 PM   #13
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
Quote:
Originally Posted by SigTerm View Post
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.
 
Old 11-02-2011, 01:28 PM   #14
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
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?
 
Old 11-02-2011, 01:30 PM   #15
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
Quote:
Originally Posted by NevemTeve View Post
Re-read it again, you will find these:
man 2 mkdir
mkdir ("buzz", 0755);
Thanks,
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create new directory?? Jorine Programming 4 12-01-2004 12:38 AM
can't create new directory dummoi Linux - Newbie 4 06-24-2004 02:50 PM
Re-create /proc directory? calabash Red Hat 3 02-18-2004 12:35 AM
Can't create new directory!!?? catty Linux - Newbie 7 10-06-2003 05:33 PM
Create a directory SnowSurfAir Linux - Software 15 07-21-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration