将字符变量添加到字符变量
add char variable to char variable
我正在尝试在新目录中创建一个文件,但我首先获取了目录的路径,然后是文件名,但是当我尝试使用文件名创建目录时,它失败了,因为我无法将两个变量添加到 mkdir mkdir (direccionarchivo,'/',nombrearchivo);
#include <iostream>
#include <fstream>
#include <io.h> // For access().
#include <sys/types.h> // For stat().
#include <sys/stat.h> // For stat().
#include <string>
using namespace std;
int main() {
char respuesta,salida,direccionarchivo[100],nombrearchivo[100];
salida = 'e';
do
{
cout << "Escoja donde desea crear el archivo de notas" << endl;
cout << "Recuerde poner todo el directorio donde desea que se cree el archivo." << endl;
cout << "Ejemplo: C:\Users\omartinr\Desktop" << endl;
cin >> direccionarchivo;
if ( access( direccionarchivo, 0 ) == 0 )
{
struct stat status;
stat( direccionarchivo, &status );
if ( status.st_mode & S_IFDIR )
{
cout << "El directorio si existe" << endl;
}
else
{
cout << "Esta direccion es un archivo" << endl;
}
}
else
{
cout << "La direccion escrita no existe" << endl;
cout << "Desea que sea creada?(S/N)" << endl;
cin >> respuesta;
if (respuesta == 's' || respuesta == 'S')
{
salida = 'f';
}
}
}while(salida == 'e');
cout << "Escriba el nombre del archivo con su tipo" << endl;
cout << "Ejemplo: notas.txt" << endl;
cin >> nombrearchivo;
mkdir (direccionarchivo,'/',nombrearchivo);
return 0;
}
使用 mkdir 时需要考虑几件事。 首先,它不能用于在目录路径中创建多个目录。 所以mkdir("/tmp/blah/foo")
,如果目录 tmp 和 blah 尚不存在,则会失败。 下面的程序是沿路径创建每个目录(如果不存在)的示例。 此外,您不希望使用 mkdir 来创建文件名,因为它将创建为目录而不是文件。 您需要使用 open() 或 fopen() 来创建/打开文件。 mkdir 的 Linux 手册页描述了这两个参数:
NAME
mkdir -- make a directory file
SYNOPSIS
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);
DESCRIPTION
The directory path is created with the access permissions specified by
mode and restricted by the umask(2) of the calling process. See chmod(2)
for the possible permission bit masks for mode.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int main (int argc, const char * argv[])
{
const char *path = "/tmp/blah/foo/bar/";
char bpath[1024];
sprintf(bpath, "%s", path);
printf("creating path %sn", bpath);
char *s = bpath;
int scnt = 0;
int first = 1;
while (*s) {
if (*s == '/') {
if (scnt == 0 && first) {
/* skip root */
} else {
/* terminate partial path */
*s = ' ';
/* The behavior of mkdir is undefined for anything other than the "permission" bits */
struct stat sbuf;
if (stat(bpath, &sbuf) == -1) {
printf("creating sub path %sn", bpath);
if (mkdir(bpath, 0777)) {
perror(bpath);
return -1;
}
} else {
printf("existing sub path %sn", bpath);
}
/* restore partial path */
*s = '/';
}
}
/* advance to next char in the directory path */
first = 0;
++s;
++scnt;
}
return 0;
}
这是该程序的输出:
creating path /tmp/blah/foo/bar/
existing sub path /tmp
existing sub path /tmp/blah
creating sub path /tmp/blah/foo
creating sub path /tmp/blah/foo/bar
如果你想连接(即连接)两个字符串,你可以使用strcat
函数。必须先创建目录,然后创建其中的文件。不能直接在不存在的目录中创建文件。从命令行可以使用mkdir -p
另外,您确定要调用的mkdir
函数吗?手册页给出了以下原型: int mkdir(const char *path, mode_t mode);
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将布尔变量添加到 int
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 尝试将带有值的不同变量添加到一小串程序C++中
- 如何从字符串中找到字符(变量)?
- C++理解计算字符变量的问题
- 将不同类型的变量添加到向量中
- 将十六进制值设置为用于填充的字符变量时出现问题
- 将字符串变量分配给无符号字符变量
- 当对字符变量使用toupper()时,所述char变量输出多个字符.我该如何防止这种情况发生
- C++无法打印字符变量
- 在字符串中的字符前添加空格C++插入函数
- 如何查看两个字符变量是否相等
- 在 c++ 中向数组变量添加一个数字
- 为什么不能在模板函数中向局部变量添加低级 const 类型
- C++将局部变量添加到静态向量而不复制
- 如果语句未正确读取字符变量
- 使用CIN和COUT时,字符变量无法正确打印
- C++将字符变量转换为字符串变量将返回 ASCII 值
- 将字符变量添加到字符变量