将字符变量添加到字符变量

add char variable to char variable

本文关键字:字符变量 添加      更新时间:2023-10-16

我正在尝试在新目录中创建一个文件,但我首先获取了目录的路径,然后是文件名,但是当我尝试使用文件名创建目录时,它失败了,因为我无法将两个变量添加到 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);