Home > OS >  How do I pass in a file name when creating them in C ?
How do I pass in a file name when creating them in C ?

Time:12-20

I am newer to C and I am trying to make a simple log in system. I am currently working on the signing up a new user and I want to create a directory for that user that I can then save their information in later. I am able to make a directory called "User" but instead of User I want to pass in an argument but I am not finding anyway to do that. Any advice?

#include <string>
#include <iostream>
#include <array>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc  .h>

#include "User.h"

User::User()
{
        //default values
    user_name = "user";
    password = "12345";
}

void User::CreateNewUser()
{
    PromptForNewUserName();
    MakeNewUserDir();
    
}

void User::SetUserName(std::string new_user_name)
{
    user_name = new_user_name;
}

void User::SetPassword(std::string new_password)
{
    password = new_password;
}

std::string User::GetName()
{
    return user_name;
}
    
void User::PromptForNewUserName()
{
    bool is_new_name = false;
    std::string temp_user_name = "user";
    do
    {
        std::cout << "Enter your name: ";
        std::cin >> temp_user_name;
        if(GetName() != temp_user_name)
        {
            SetUserName(temp_user_name);
            is_new_name = true;
        }else {std::cout << "That user name is already in use." << std::endl; is_new_name = false;}
    }while(!is_new_name);
}

void User::PromptForNewPassword()
{
    
}

void User::PasswordReset()
{
    
}

void User::MakeNewUserDir()
{
    if(!GetName().empty())
    {
        int check;
        int name_length = GetName().length();
        char name_array[name_length   1];
            // copies get name in to char array since mkdir takes in a char not a string
        strcpy(name_array, GetName().c_str());
            // mkdir returns an int, so we can assign it to check
        check = mkdir("C:/Users/3192833/Documents/MobaXterm/Lee/LinuxCheater/User", 0777);
        
            // check if the directory is created or not
        if(!check)
        {
            std::cout << std::endl;
        
            const char* name_array_ptr = name_array;
            rename("User", name_array_ptr);
            std::cout << "\nAn account has been created for " << GetName() << std::endl;
        }else{
            printf("Unable to create account\n");
            exit(1);
        }
        
    }
}

CodePudding user response:

Add #include <string> to the top of your source file

Change your MakeNewUserDir() function to be declared and defined as follows:

void User::MakeNewUserDir(const std::string& path)

You may need to make a similar change in your class declaration as well.

Then you invoke mkdir as follows:

check = mkdir(path.c_str(), 0777);

And then later on, when you want to invoke MakeNewUserDir with an argument, you can pass a string literal, another instace of std::string, or a char* based C string.

User u; 
u.MakeNewUserDir("C:/Users/3192833/Documents/MobaXterm/Lee/LinuxCheater/User");

OR

std::string s = "C:/Users/3192833/Documents/MobaXterm/Lee/LinuxCheater";
s  = "/User";
u.MakeNewuserDir(s);
  • Related