如何使一个文件夹/目录 [英] How to make a folder/directory
问题描述
如何使用c ++创建目录/文件夹。我试图使用mkdir()没有成功。我想编写一个程序,cin是一个变量,然后使用这个变量来创建子目录和文件。
我当前的代码。它说的mkdir()中的+运算符说错误没有操作数
char newFolder [20]
cout<< 输入新项目的名称,不含空格:\\\
;
cin>>新文件夹;
string files [] = {index.php};
string dir [] = {/ images,/ includes,/ includes / js,/ contact,about};
for(int i = 0; i <= 5; i ++){
mkdir(newFolder + dir [i]);
ofstream write(index / index.php);
write<< <?php \\\
\\\
\\\
?>;
write.close();
}
$ c> #include< string> , std :: string
表达式 newFolder + dir [i]
的结果是 std :: string
, mkdir()
需要一个 const char *
。更改为:
mkdir((newFolder + dir [i])c_str
检查 mkdir()的返回值
以确保成功,如果不使用 strerror(errno)
来获取失败的原因。
数组的结尾 dir
:
for(int i = 0; i <= 5; i ++){
mkdir(newFolder + dir [i]);
5
$ c> dir ,所以合法索引从 0
到 4
。更改为:
for(int i = 0; i <5; i ++){
mkdir(newFolder + dir [一世]);
使用 std :: string
c $ c> newFolder ,而不是 char [20]
:
std :: string newFolder;
然后你不必担心超过19个字符
How do I make a directory/folder with c++. I've tried to use the mkdir() without success. I want to write a program that cin's a variable and then uses this variable to create sub-directory's and files with in those. my current code. It says the + operator in the mkdir() says error no operand
char newFolder[20];
cout << "Enter name of new project without spaces:\n";
cin >> newFolder;
string files[] = {"index.php"};
string dir[] = {"/images","/includes","/includes/js","/contact","about"};
for (int i = 0; i<=5; i++){
mkdir(newFolder + dir[i]);
ofstream write ("index/index.php");
write << "<?php \n \n \n ?>";
write.close();
}
You need to #include <string>
, the std::string
operators are defined in that header.
The result of the expression newFolder + dir[i]
is a std::string
, and mkdir()
takes a const char*
. Change to:
mkdir((newFolder + dir[i]).c_str());
Check the return value of mkdir()
to ensure success, if not use strerror(errno)
to obtain the reason for failure.
This accesses beyond the end of the array dir
:
for (int i = 0; i<=5; i++){
mkdir(newFolder + dir[i]);
there are 5
elements in dir
, so legal indexes are from 0
to 4
. Change to:
for (int i = 0; i<5; i++){
mkdir(newFolder + dir[i]);
Usestd::string
for newFolder
, rather than char[20]
:
std::string newFolder;
Then you have no concern over a folder of more than 19 characters (1 required for null terminator) being entered.
这篇关于如何使一个文件夹/目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!