简体   繁体   中英

add char variable to char variable

I am trying to create a file in a new directory, but i am getting the path to the directory first and then the file name, but when i try to create the directory with the file name it fail, because i can't add both variables to 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;
}

If you want to concatenate(that is join) two strings you can use the strcat function. You must first create the directory and then the file inside it. You cannot directly create the file in a non-existent directory. From the command line its possible using mkdir -p

Also are you sure about the mkdir function you are calling? The man page gives the following prototype: int mkdir(const char *path, mode_t mode);

There are a couple of things to consider when using mkdir. First it can not be used to create more than one directory in the directory path. So mkdir("/tmp/blah/foo") , will fail if directories tmp and blah don't already exist. The below program is an example of creating each directory along the path if it doesn't exist. Also you do not want to use mkdir to create the filename as it will be created as a directory and not a file. you will need to use open() or fopen() to create/open the file. The linux man pages for mkdir describe the two parameters:

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;
}

Here is the output from this program:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM