Question:
c++ file question, 10 points most helpful answer, source code included. thanks.?
Bond Man
2009-10-03 10:46:17 UTC
So what i want to do is create multiple folders though a function calling its self. so what i am trying to do is make the folders have a start name that user types in, like foobar, and then the the program asks for the number of folders to create. say the number entered is 4, then program starts making folders like c:/foobar/foobar1/foobar2/foobar3/foobar4/. any ideas are helpful, um i believe that this requires me to make an int to string conversion. so i have combine stings. and well so ya... here is kinda what i got. these are called by the main file, in the main function. so ya.


//Create Multiplie directories named in a certain order.
#include
#include
#include
#include
#include // for itoa() call
#include
using namespace std;

void CreateNumberDirectories(int foldernumb, string foldername, string folderhold = "/")// string directoryname)//middle char []
{
int foldernum = foldernumb;
string folderpath = foldername;
string buf;
string path = "c:/"; //the beginning of the path, will combine this with the folder
//names and number
itoa(foldernumb, buf, 10); //int to string conversion, doesnt work so well.
cout << path << endl; //this is here to show me the path so i know what i want.
//Here is where i call the same function CreateNumberDirectories(...)
//to create more folders.
}


void StartDirectoryCreation()
{
int foldernum; //the number of folders to create
string foldernames; //the folders beginning name
cout << "Folder names: ";
cin >> foldernames; //assing the folder names
cout << "iteration: ";
cin >> foldernum; //assinging the nuimber of folders
CreateNumberDirectories(foldernum, foldernames); //calls the folder creation funtion.
}
Three answers:
Mark aka jack573
2009-10-07 06:53:39 UTC
Ok, first up, to actually make the directories (folders) on your hard drive, you may to need to use functions that are dependant on the operating system you are using. I have not done anything like this, so I cannot help you there.



What I can do is write an algorithm that does what you want. Then write some code that does what the algorithm does.



Ok, what you should do is:

- Create a function that takes the folowing parameters.

string currrentFolder // This is the folder that you want to create the new folder into. When you call the function the first time, it should be C:\\ or C:/ If you need to make a string or char that is \, you do it this way \\

string nameOfFolder; // The name of the folder you want to create, like foobar.

int numberOfFoldersTotal; // This would be 4 all the time if you entered 4.

int numberOfFoldersLeft; // This is how many folders are left to create. So if it is 0 or less, you stop the recursion.

// If you want the folder hold parameter, you would use either "\\" or "/" I have not said that this is a required parameter, but you may want it to be. If it is a requried parameter, you should pass in C: as the initial folder instead of C:\\ or C:/ as I said above.



- In this function, it should do something like this.

-- if number of folders left is 0

--- return;

-- end if

-- make a new string with the folder name

-- make an int of the number of folders total minus the number of folders left

-- if the int is > 0

--- add this number to the string of the folder name.

-- end if

-- create the folder using the folder name you just created. If you are actually making the folder, use the name of the current folder as the place to make the new folder.

-- append the current folder with the folder hold (as you call it) and the new name of the folder

-- call the function again (recursion) with the same parameters, except the current folder has changed and the number of folders left should be one less.



- Use a function to convert an int to a string as you said itoa did not really work for you.



- Then in you main, you get the user input and call the function. You should not have to have to call one function that then calls the recursive function as your code seems to do.



Now for the code:



string intToString(int n);

void createFolders(string currentFolder, string nameOfFolder, int numberOfFoldersTotal, int numberOfFoldersLeft, string folderHold = "\\"); // or "/" for the folderHold.



int main()

{

string currentFolder = "C:\\"; // "C:/";

string nameOfFolder;

int numberOfFoldersTotal;

// get the input for the name of the folder and the number of folders.

createFolders( currentFolder, nameOfFolder, numberOfFoldersTotal, numberOfFoldersTotal + 1);

return 0;

}



void createFolders(string currentFolder, string nameOfFolder, int numberOfFoldersTotal, int numberOfFoldersLeft, string folderHold = "\\") // or "/" for the folderHold.

{

if (numberOfFoldersLeft <= 0)

return;

}

string folderNameNew = nameOfFolder;

int left = (numberOfFoldersTotal - numberOfFoldersLeft) + 1;

if (left > 0)

{

folderNameNew += intToString( left );

}

// create the folder using the folder name you just created. If you are actually making the folder, use the name of the current folder as the place to make the new folder. Do it in this line, I can't help with this :(

currentFolder += folderNameNew + folderHold;

// If you want to printout the name fo the folder, do it here using the currentFolder as the name

createFolder( currentFolder, nameOfFolder, numberOfFoldersTotal, numberOfFoldersLeft - 1, folderHold );

}



string intToString(int n)

{

ostringstream ss; // You need to include sstream for this.

ss << n;

return ss.str();

}



Of course, you might want to do some checking of the inputs from the user, to make sure the name of the folder would be valid, the number of folders to create is > 0, etc.



Good luck with it.
kigar
2016-11-05 11:50:35 UTC
attempt making your nums each of a similar information form. you're changing a double long (which must be long double) right into a waft and then exhibiting the double long in case you in hassle-free terms pick 5 decimal factors you in hassle-free terms want floats, attempt: int considerable() { waft num1; waft num2; string yesOrNo = "specific"; good success
2009-10-07 07:03:03 UTC
Hm....



















Product folder names:

char folder[80]="c:/foobar";

char *temp=new char[80];

for(int i=1;i<4;i++){

sprintf(temp,"%s/foobar%0d",folder,i);

sprintf(folder,"%s",temp);

}

delete temp;



result folder name


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...