【问题标题】:add char variable to char variable将 char 变量添加到 char 变量
【发布时间】:2013-11-09 04:57:59
【问题描述】:

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

【问题讨论】:

  • 确切的错误是什么?你到底想做什么?此外,从长远来看,用英语编码(即使 UI 是另一种语言)对您非常有益。 StackOverflow 更容易理解你的程序,而且它可能会帮助你的职业生涯......
  • 如果你使用 std::string,你会发现这很容易(而且更不容易出错)。

标签: c++ path makefile char


【解决方案1】:

使用 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 %s\n", 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 = '\0';
                /* 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 %s\n", bpath);
                    if (mkdir(bpath, 0777)) {
                        perror(bpath);
                        return -1;
                    }
                } else {
                    printf("existing sub path %s\n", 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

【讨论】:

    【解决方案2】:

    如果您想连接(即连接)两个字符串,您可以使用strcat 函数。 您必须首先创建目录,然后在其中创建文件。您不能直接在不存在的目录中创建文件。从命令行可以使用mkdir -p

    您还确定要调用的mkdir 函数吗?手册页给出了以下原型:int mkdir(const char *path, mode_t mode);

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 2023-03-08
      • 2013-03-30
      • 2021-01-24
      • 2021-12-12
      相关资源
      最近更新 更多