在 C++ 中创建文件时如何传入文件名?
How do I pass in a file name when creating them in C++?
我是 C++ 的新手,我正在尝试制作一个简单的登录系统。我目前正在注册一个新用户,我想为该用户创建一个目录,稍后我可以在其中保存他们的信息。我可以创建一个名为“用户”的目录,但我想传递一个参数而不是用户,但无论如何我都找不到这样做。有什么建议吗?
#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);
}
}
}
将 #include <string>
添加到源文件的顶部
更改要声明和定义的 MakeNewUserDir() 函数,如下所示:
void User::MakeNewUserDir(const std::string& path)
您可能还需要在 class 声明中进行类似的更改。
然后按如下方式调用 mkdir
:
check = mkdir(path.c_str(), 0777);
稍后,当您想要使用参数调用 MakeNewUserDir 时,您可以传递字符串文字、std::string 的另一个实例或基于 char*
的 C 字符串。
User u;
u.MakeNewUserDir("C:/Users/3192833/Documents/MobaXterm/Lee/LinuxCheater/User");
或
std::string s = "C:/Users/3192833/Documents/MobaXterm/Lee/LinuxCheater";
s += "/User";
u.MakeNewuserDir(s);
我是 C++ 的新手,我正在尝试制作一个简单的登录系统。我目前正在注册一个新用户,我想为该用户创建一个目录,稍后我可以在其中保存他们的信息。我可以创建一个名为“用户”的目录,但我想传递一个参数而不是用户,但无论如何我都找不到这样做。有什么建议吗?
#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);
}
}
}
将 #include <string>
添加到源文件的顶部
更改要声明和定义的 MakeNewUserDir() 函数,如下所示:
void User::MakeNewUserDir(const std::string& path)
您可能还需要在 class 声明中进行类似的更改。
然后按如下方式调用 mkdir
:
check = mkdir(path.c_str(), 0777);
稍后,当您想要使用参数调用 MakeNewUserDir 时,您可以传递字符串文字、std::string 的另一个实例或基于 char*
的 C 字符串。
User u;
u.MakeNewUserDir("C:/Users/3192833/Documents/MobaXterm/Lee/LinuxCheater/User");
或
std::string s = "C:/Users/3192833/Documents/MobaXterm/Lee/LinuxCheater";
s += "/User";
u.MakeNewuserDir(s);