Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,383 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,191 people online right now. Registration is fast and FREE... Join Now!




Creating Directories

2 Pages V  1 2 >  
Reply to this topicStart new topic

Creating Directories

Techno Mage
post 31 Aug, 2008 - 06:02 AM
Post #1


D.I.C Head

**
Joined: 10 Jun, 2007
Posts: 58


My Contributions


I'm having a real problem with mkdir(). I think I'm getting a segfault from it.

First off, I can't find it in any reference guide (well, no real info on the net, not even on cplusplus.com).

Second, what are the possible codes for for mkdir? Can't find them anywhere! I have no access to this directory I create either. Like, I click on it and I'm told I don't have the permissions to access it. I'm pretty sure it's something to do with the second parameter of mkdir. This is Linux by the way.

Do I have to "open" a folder in order to write to it? I'm trying to make this directory and just write files to it but now I think that one problem is that I haven't opened the directory.

PS: This is unrelated but how can I enter a number into a string and have it increment? I was having a problem with that.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 31 Aug, 2008 - 06:10 AM
Post #2


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


I've never used mkdir. Is it a variadic function? If it is, make sure you're providing all parameters.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 31 Aug, 2008 - 06:17 AM
Post #3


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


It doesn't appear that it is (unlike open()). I don't know. We might actually need to see what you did.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 31 Aug, 2008 - 07:04 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


ohmy.gif I can help on a question that perfectly.insane was unsure of!

*feels proud*

It's as simple as calling mkdir();
All you need to do is specify a path. You don't need a directory to be open, or anything like that. Simple!

Here's a basic example:
cpp
#include <dirent.h>

int main ()
{
mkdir(".\\gabehabe");
return 0;
}


See how simple it is? smile.gif

Also, you may find the following tutorials helpful:
Accessing directories in C/C++ Part I
Accessing directories in C/C++ Part II

Hope this helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 31 Aug, 2008 - 07:28 AM
Post #5


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


LOL. That won't work on Unix. The Unix version requires a mode parameter. The Windows version doesn't. That's what the reference to the variadic function was about. open() is a variadic function, and it also has a mode parameter, which is required depending on the other parameters passed to open. The compiler doesn't catch this kind of thing, except when programmed specifically to do so (like with printf). It can cause some strange behaviors, such as creating files with strange permission sets (being unable to open the file after writing it).

This post has been edited by perfectly.insane: 31 Aug, 2008 - 07:30 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 31 Aug, 2008 - 07:33 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


awwwww... sad.gif
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 31 Aug, 2008 - 07:35 AM
Post #7


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


You're just going to have to wait for him to post his code... smile.gif (It segfaults, after all).
User is offlineProfile CardPM

Go to the top of the page

Techno Mage
post 31 Aug, 2008 - 05:26 PM
Post #8


D.I.C Head

**
Joined: 10 Jun, 2007
Posts: 58


My Contributions


Again, does anyone have a list of possible parameters to mkdir? Also, do I have to "open" a directory to write files to it?Here's my code:
cpp

//Fix fileName
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sys/stat.h>
#include <string>
#include <fstream>
//#include <direct.h>

using namespace std;

struct stat fileResults;

//Tests if the file exists
bool fileExists(string fName)
{
if(stat(fName.c_str(),&fileResults)==0)
return true;
else
return false;
}

//char** fileCutter(ifstream& input)
string fileCutter(ifstream& input)
{

//char** pieces;
//*pieces = (char*)malloc((fileResults.st_size/3800)*sizeof(char*));
string pieces;
/*
for(int i = 0; i<(fileResults.st_size/3800);i++)
{
//*(pieces+i) = (char*)malloc(3800*sizeof(char));
for(int k = 0; k<3800;k++)
{
pieces = pieces+(char)input.get();
//*(*(pieces+i)+k) = (char)input.get();
}
}*/
for(int i = 0;i<fileResults.st_size - 1;i++)
{
pieces = pieces+(char)input.get();
}
return pieces;
}

//void fileWriter(char** pieces, char* name)
string fileWriter(string pieces, string name)
{
//char** fpieces = pieces;
string fpieces = pieces;
ofstream outFile;
string fName;
string fNum[1000];
for(int i = 0;i<1000;i++)
{
fNum[i] = (char)i;
}

for(int i = 0; i<(fileResults.st_size/3800);i++)
{
fName = name+"/part_1.txt";
//fName = fName+"/part_"+(char)i+".txt";
cout<<fName<<"\n";
outFile.open(fName.c_str());

for(int k = 0; k < 3800;k++)
{
outFile << fpieces.at(k+(3799*i));
}
outFile.close();
}

//cout << fpieces<<"\n";
//outFile << fpieces;
}
//Cuts excess from filename and extracts name to be used as a directory
string directoryName(string file)
{
string retString = file;
int position = 0;

position = file.find("Documents");
if (position == -1)
throw("Documents folder doesn't exist. O_o");
retString = retString.substr(position+10,retString.find(".")-(position+10));
cout << retString << "\n";
return retString;
}

/*
void fileCutter(ifstream& input, string name)
{
int charCount = 0;
int fNum = 1;
string fstr;
ofstream outFile;
//Writes the files
while(input.eof()==false)
{
//Sets string to null
fstr=NULL;
//Places 3850 characters in a string to be written to file
//while 3850 hasn't been reached or the end of the file
do
{
strcat(fstr, input.get());
charCount++;
}while(charCount%3850!=0 && input.eof()==false);
outFile.open(name+"part_"+fnum+".txt", ios::trunc);
outfile << fstr;
outFile.close();
fNum++;
}
}
*/

int main(int argc, char* argv[])
{
//Checks to see if there is was an argument through commandline
string fileName;
string dirName;
if(argc>1)
fileName = (string)*(argv+1);
long size = 0;
ifstream inFile;
int fileCount = 0;
//Opens file depending on if there's input from terminal or not.
try
{
if(argc>1)
if(fileExists(fileName)==true)
{
//Outputs size of file and opens it
inFile.open(fileName.c_str());
size = fileResults.st_size;
cout << size<<endl;
}
else
throw string("File does not exist.\n");
else
{
cout << "Enter the name of the file:\n";
//Accepts input for fileName but also allocates 1 more space
//for a null character

cin >> fileName;

if(fileExists(fileName)==true)
{
//Outputs size of file and opens it
//While throwing an exception if we can't open the file
inFile.open(fileName.c_str());
if(inFile.bad() == true)
throw string("Cannot open file.");
size = fileResults.st_size;
cout << size<<endl;
}
else
throw string("File does not exist.\n");
}
//If fileName really is a file but not a text file
//throw an error.
if(strcasestr(fileName.c_str(), ".txt")==NULL)
throw string("Not a text file.\n");
dirName = "/home/tetrix/Documents/"+directoryName(fileName);
cout<<dirName<<endl;
//dirName = strcat(dirName, fileName.c_str());
//Make a directory with the name of the file being read
mkdir(dirName.c_str(), 0777);
cout << "Directory successfully created"<< endl;
fileWriter(fileCutter(inFile),dirName.c_str());
//fileCutter(inFile,"/home/tetrix/Documents/"+fileName+"/");
cout << "File cut!";
}

catch(string error)
{
cout << "Error: " << error;
}
catch(bad_alloc mError)
{
cout << "Memory Allocation error";
}
return 0;
}

Segfault is around mkdir, so skip to around there. Actually, it's during the fileWriter function, but I think it's related to mkdir.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 31 Aug, 2008 - 10:39 PM
Post #9


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


This is the definition of mkdir that you need to be worried about:

http://www.opengroup.org/onlinepubs/000095...ions/mkdir.html

No, you don't need to "open" the directory to write files. You only do that when you need to access the index directly (listing files).

I'm not really sure what you're trying to do here. If you want to read the entire file, there are some way easier ways of doing it.

ifstream ifs(...);

// Create some array the size of the file...
char* buf = new char[filesize];
ifs.read(buf, filesize);
// or ifs.rdbuf()->sgetn(buf, filesize);
ifs.close();

As far as the segfault, I'm not sure what's causing that, but I do know that you should call outFile.clear() at the end of your loop in the fileWriter function if you want to keep using the same ofstream object multiple times.
User is offlineProfile CardPM

Go to the top of the page

Techno Mage
post 1 Sep, 2008 - 05:44 AM
Post #10


D.I.C Head

**
Joined: 10 Jun, 2007
Posts: 58


My Contributions


Thanks man. I'll use that method for reading a file. I'm still new at this so it'll be a while before I can write cryptic yet efficient code.

Anyway, my objective is to read a file, and then write it to a file, creating a new file every 3.8kb, and stick all of those files into a folder. It's for the iPod, to get over it's 4kb limit.

For the directory modes, I read the thing and what I'm getting is that it's the same as file permission in Linux. Is that right?
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 1 Sep, 2008 - 06:31 AM
Post #11


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


The mode of the directory you create is affected by umask. Make sure that that is set correctly if you're using any strange umask.

This simple test program seems to operate as expected for me:

cpp


#include <sys/stat.h>
#include <iostream>
#include <string.h>
#include <errno.h>

int main(int argc, char** argv)
{
using namespace std;

// If user doesn't give enough arguments, die.
if(argc != 2) {
cerr << "Usage: " << argv[0] << " directory" << endl;
return 1;
}

// Directory is first given argument
const char* dirname = argv[1];
mode_t mode = 0777;

if(0 != mkdir(dirname, mode)) {
cerr << argv[0] << ": " << strerror(errno) << endl;
return 1;
}

return 0;
}


By the way... have you tried the split utility (I think it already does what you're trying to do)?
User is offlineProfile CardPM

Go to the top of the page

Techno Mage
post 1 Sep, 2008 - 08:11 AM
Post #12


D.I.C Head

**
Joined: 10 Jun, 2007
Posts: 58


My Contributions


QUOTE(perfectly.insane @ 1 Sep, 2008 - 10:31 AM) *

The mode of the directory you create is affected by umask. Make sure that that is set correctly if you're using any strange umask.

This simple test program seems to operate as expected for me:

cpp


#include <sys/stat.h>
#include <iostream>
#include <string.h>
#include <errno.h>

int main(int argc, char** argv)
{
using namespace std;

// If user doesn't give enough arguments, die.
if(argc != 2) {
cerr << "Usage: " << argv[0] << " directory" << endl;
return 1;
}

// Directory is first given argument
const char* dirname = argv[1];
mode_t mode = 0777;

if(0 != mkdir(dirname, mode)) {
cerr << argv[0] << ": " << strerror(errno) << endl;
return 1;
}

return 0;
}


By the way... have you tried the split utility (I think it already does what you're trying to do)?

So yeah, it's the octal for permissions in linux. However, when I try to go to it, I'm still told that I don't have permission for opening the folder. Do I have to have mode_t mode = 0777? I was just putting 0777 straight into the second parameter.

I haven't tried the split utility. I imagine it would be easier to use. What is it in, boost? How do I download and use other libraries besides the standard library.

Before I go on my merry way though, I really want to know why is being retarded and giving me a segfault and how to fix it. You know, for learning purposes.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/22/08 06:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month