25249

Create a directory

I am trying to create a directory using the following code. It compiles, but it does not create a directory. Any suggestions?

#include <stdio.h> #include <string.h> #include <sys/stat.h> int main(void) { const char base[] = "filename"; char filename [ FILENAME_MAX ]; int number = 42; sprintf(filename, "%s/%d", base, number); printf("filename = \"%s\"\n", filename); mkdir (filename, S_IRWXU); return 0; }

Answer1:

Does the "filename" directory already exist? mkdir() will only create one directory at a time; if the parent directory doesn't exist either, you'll have to create it separately, first.

Answer2:

Most probably it fails to create directory because you are trying to create a nested directory and its parent does not exist. mkdir cannot create directories recursively. But you can only guess unless you properly check return codes and errors in your program.

Recommend

  • How to receive a zip file from server: C/C++
  • How can I find the Size of some specified files?
  • Beginner in SQLite
  • R encoding ASCII backtick
  • Declare struct containing time_t field in Python CFFI
  • Arduino making decision according to a packet received from serial port
  • date format change with DT and shiny
  • Invert string in Rust
  • Changing a global variable in C
  • Bad interaction between Zope2 XML-RPC and AT Image mutator?
  • How to unpack 32bit integer packed in a QByteArray?
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Getting short path in python
  • How to read piped content in C?
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Object and struct member access and address offset calculation
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • how to upload multiple files in c# windows application
  • Appending Character to Character Array In C
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • output of program is not same as passed argument
  • How to handle AllServersUnavailable Exception
  • Convert array of 8 bytes to signed long in C++
  • Akka Routing: Reply's send to router ends up as dead letters
  • Linker errors when using intrinsic function via function pointer
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • LevelDB C iterator
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?