Dynamic directory names

This is a discussion on Dynamic directory names within the C++ Programming forums, part of the General Programming Boards category; Hi, As some know from my previous post, I was attempting to create folders, or directories. That is no longer ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Question Dynamic directory names

    Hi,

    As some know from my previous post, I was attempting to create folders, or directories. That is no longer the problem.

    What I am wondering about now is how to assign the directories dynamic names, i.e: collecting a string from the user using cin<<variable<<endl; and assigning it to a folder name.

    Right now I am wanting to collect a 30 character string from the user, tell the program to create a directory like so:

    mkdir("C:/Program Files/Program Name/"<<userinputstring<<"/");

    It always results in an error (I would post it, but I am about to shut down, and Dev-C++ is no longer running...) every time I try to use a variable string in a location. Is there any way around this?

    Thanks so much!

    FlyingIsFun1217

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Use a std::string to build up the whole path

    std::string newDir = "C:/Program Files/Program Name/" + userInput;

    Then use the c_str() method to get the const char * version mkdir expects

    mkdir( newDir.c_str() );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Thanks, I will try using that

    FlyingIsFun1217
    -------------------------------EDIT-----------------------------
    I tried that, but when I attempt to compile it, it says:
    ---'userinput' undeclared (first use this function)---

    I'm using Dev-C++

    If theres something here that Im missing, sorry, I'm somewhat new to c++

    Thanks again!

    FlyingIsFun1217
    Last edited by FlyingIsFun1217; 10-21-2006 at 09:51 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > ---'userinput' undeclared (first use this function)---
    See, you should really have figured out to use your own variable name rather than the one I used.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Yes, I did wonder about that, but when I tried using a 30 character string named newusername (which is used to collect the user name), it gives me this error:

    ---invalid operands of types 'const char[22]' and 'char[30]' to binary 'operator+'---

    Should I have changed something else too?

    Thanks again!

    FlyingIsFun1217

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Yeah, one of them should have been a std::string as per my original post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should be using std::string in general instead of character arrays. It will make your coding a lot easier. In this case, you can't use + to combine character arrays, but if you have a C++ string you can easily.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    even when I change the variables to std::string newusername[30] and std::string newuserpassword[30], it still gives me the same error...

    Thanks for your patience

    FlyingIsFun1217

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Instructions, std::string, not std::string [30].

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > std::string newusername[30]
    Would be an array of strings, not just one string.

    std::string replaces a char array.

    char a[10]; would be std::string a;
    char b[20][10]; would be std::string b[20];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Ok, thanks for the help!

    Now my next problem is creating a text file in that directory. What I basically do with the program is ask for a username & password, and make a directory with that that contains a username & password file in that directory.

    Now that I have created a directory with the username, I need to open/create files in it, which I had previously done with:

    Code:
    ofstream userpasswordcreate;
        userpasswordcreate.open("C:/Program Files/MUMS/Administrator/password.mums");
        userpasswordcreate<<newuserpassword<<endl;
        userpasswordcreate.close();
    but now, I would need to change 'Administrator' into the string 'newusername' to open the right directory. How would I go about doing this? When I try:

    Code:
    ofstream userpasswordcreate;
        userpasswordcreate.open("C:/Program Files/MUMS/"<<newusername<<"/password.mums");
        userpasswordcreate<<newuserpassword<<endl;
        userpasswordcreate.close();
    it tells me:

    no match for 'operator<<' in '"C:/Program Files/MUMS/" << newusername'
    how would I go about using the string in the path name?

    Thanks!

    FlyingIsFun1217

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > userpasswordcreate.open("C:/Program Files/MUMS/"<<newusername<<"/password.mums");
    You need to create a string first

    Code:
    std::string fname = "C:/Program Files/MUMS/";
    fname += newusername;
    fname += "/password.mums";
    userpasswordcreate.open( fname.c_str() );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Ok, that seems to be the solution. Even after I use that though, It still seems to not want to create a directory with that username. Heres my *completed code so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
    
        std::string newusername;
        std::string newuserpassword;
        std::string newDir="C/Program Files/MUMS/"+newusername;
        
        std::string unfile="C:/Program Files/MUMS/";
        unfile+=newusername;
        unfile+="/username.mums";
        
        std::string pfile="C:/Program Files/MUMS/";
        pfile+=newuserpassword;
        pfile+="/password.mums";
    
        {
        cout<<"Please enter a user name to use (one word only, 30 letters): ";
        cin>>newusername;
        cin.ignore();
        
        cout<<"\n"<<newusername<<" is now being used as your username.\n\n";
        cout<<"Enter a password to use for "<<newusername<<"'s account: ";
        cin>>newuserpassword;
        cin.ignore();
        
        cout<<"\nPlease wait while your account ('"<<newusername<<"') is created...";
        cout<<"\n\nCreating User Folder...";
        
        mkdir("C:/Program Files/MUMS/");
        mkdir(newDir.c_str());
        
        cout<<"\nCreating Username File...";
        //    created username file
        {
        ofstream usernamecreate;
        usernamecreate.open(unfile.c_str());
        usernamecreate<<newusername<<endl;
        usernamecreate.close();
        }
        
        cout<<"\n"<<newusername<<"'s User Name file successfully created!...";
        
        cout<<"\nCreating Password File...";
        {
        ofstream userpasswordcreate;
        userpasswordcreate.open(pfile.c_str());
        userpasswordcreate<<newuserpassword<<endl;
        userpasswordcreate.close();
        }
        
        cout<<"\n"<<newusername<<"'s Password saved successfully!...";
        
        cout<<"\n\nDone!";
        cout<<"\n\nYour User Name is '"<<newusername<<"' and your password is '"<<newuserpassword<<"'.\n"
              "Make sure that you know these, as there is no way of retirieving\nthis information.";
        cin.get();
        }
    This code just produces a directory named MUMS, and places the password.mums and username.mums file in it though.

    Can somebody spot where I screwed up?

    Thanks again!

    FlyingIsFun1217

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > unfile+=newusername;
    Yes, you need to input the value first, before you use it to build up a directory path.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Aren't I doing that with the cin>>newusername; ?

    Heres what I have when I try repositioning the username and password file creation code:

    Code:
    //  Headers go here...
    #include <iostream>
    #include <fstream>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <stdio.h>
    
    //  using namespace goes here...
    using namespace std;
    
    //  start of main program
    int main()
    {
    
    //  variable definitions here...
        std::string newusername;
        std::string newuserpassword;
        std::string newDir="C/Program Files/MUMS/"+newusername;
    
        {
        //    ask for account name
        cout<<"Please enter a user name to use (one word only, 30 letters): ";
        cin>>newusername;
        cin.ignore();
        
        //    ask for account password
        cout<<"\n"<<newusername<<" is now being used as your username.\n\n";
        cout<<"Enter a password to use for "<<newusername<<"'s account: ";
        cin>>newuserpassword;
        cin.ignore();
        
        //    create account (including folder and file)
        cout<<"\nPlease wait while your account ('"<<newusername<<"') is created...";
        cout<<"\n\nCreating User Folder...";
        
        mkdir("C:/Program Files/MUMS/");
        mkdir(newDir.c_str());
        
        cout<<"\nCreating Username File...";
        //    created username file
        
        //  This is for the creation of the username file
        std::string unfile="C:/Program Files/MUMS/";
        unfile+=newusername;
        unfile+="/username.mums";
        
        {
        ofstream usernamecreate;
        usernamecreate.open(unfile.c_str());
        usernamecreate<<newusername<<endl;
        usernamecreate.close();
        }
        
        cout<<"\n"<<newusername<<"'s User Name file successfully created!...";
        
        cout<<"\nCreating Password File...";
        //    created password file
        
        //  This is for the creation of the password file
        std::string pfile="C:/Program Files/MUMS/";
        pfile+=newuserpassword;
        pfile+="/password.mums";
        
        {
        ofstream userpasswordcreate;
        userpasswordcreate.open(pfile.c_str());
        userpasswordcreate<<newuserpassword<<endl;
        userpasswordcreate.close();
        }
        
        cout<<"\n"<<newusername<<"'s Password saved successfully!...";
        
        cout<<"\n\nDone!";
        cout<<"\n\nYour User Name is '"<<newusername<<"' and your password is '"<<newuserpassword<<"'.\n"
              "Make sure that you know these, as there is no way of retirieving\nthis information.";
        cin.get();
        }
    }
    Thanks again,

    FlyingIsFun1217
    Last edited by FlyingIsFun1217; 10-22-2006 at 01:50 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  2. Adding a directory to a dynamic library path
    By vivharv in forum Windows Programming
    Replies: 3
    Last Post: 09-20-2007, 07:09 AM
  3. traverse a directory and list all file names?
    By George2 in forum C Programming
    Replies: 3
    Last Post: 08-24-2006, 06:18 PM
  4. File names in a directory
    By gotclout in forum C++ Programming
    Replies: 6
    Last Post: 06-28-2006, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21