This is a part of the program I'm trying to create in C++.
Ever since starting with C++ I've had a problem with different variable types. The function mkdir requires const char
I believe and I'm not sure how to convert the right variable from input to what I need.
All the variables and includes required are in the program. This is my only problem.
I might not be the most specific on my problem but I'm new to C++ and any help will be appreciated, thanks!
int createaccount(const char acc_name)
{
int status = mkdir("/home/person/Desktop/Accounts/" + acc_name, S_IRWXU);
return 0;
}
std::string new_acc_name;
cin >> new_acc_name;
new_acc_namechar = str.new_acc_namechar_str();
createaccount(new_acc_namechar);`
If you're working in C++, learn to use std::string
. It is a built-in string type which can be converted to a const char*
by calling c_str()
. Normally you would store and pass around the std::string
objects all the time unless you need to interface with something that takes a const char*
. Call c_str()
at that point. For example:
int createaccount(const std::string& acc_name)
{
std::string path = "/home/person/Desktop/Accounts/" + acc_name;
int status = mkdir(path.c_str(), S_IRWXU);
return 0;
}
And after making a guess about what your code is trying to do:
std::string new_acc_name;
cin >> new_acc_name;
createaccount(new_acc_namechar);
int createaccount(const char acc_name)
{
int status = mkdir("/home/person/Desktop/Accounts/" + acc_name, S_IRWXU);
return 0;
}
in this function, you cannot just concatenate a const char*
and a char
using +
operator. Instead you should use the int status = mkdir (string(string("/home/person/Desktop/Accounts/")+acc_name).c_str(), S_IRWXU);
mkdir()
is a libc function that is declared as (according to man 2 mkdir
):
int mkdir(const char *pathname, mode_t mode);
so indeed, it takes a const char*
as pathname. In C++, you are often dealing with std::string
rather than the low level char*
of C. you can use both to as "strings", but they are not interchangeable. std::string supports nice thingies like concatenating strings with the +
operator and many more.
in your example, you are using +
to concatenate "strings", but your strings are really char arrays, so you cannot simply concat them like that.
a simple solution would be:
int createaccount(const std::string&filename) {
std::string path="/home/person/Desktop/Accounts/";
std::string fullname=path+filename;
int status = mkdir(fullname.c_str(), S_IRWXU);
return 0;
}
which uses std::string for concatenating and the c_str()
member function of std::string to convert the C++-string to C's const char*
I think what you want is something along the lines of:
int createaccount(const char acc_name) {
std::string combinedPath = "/home/person/Desktop/Accounts" + acc_name;
int status = mkdir(combinedPath.c_str(), S_IRWXU);
return status;
}
"/home/person/Desktop/Accounts/" + acc_name
this is adding acc_name, which is a single character, to the address of the string literal "/home/person/Desktop/Accounts/"
. You want to do something like this.
int createaccount(const char acc_name)
{
std::string dir = "/home/person/Desktop/Accounts/";
dir += acc_name;
int status = mkdir(dir.c_str(), S_IRWXU);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.