Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:17:45+00:00 2026-06-06T17:17:45+00:00

I need to write a binary file to some specified directory using a C

  • 0

I need to write a binary file to some specified directory using a C program. But, while writing I want to make sure that if the directory structure does not exist it should create one for me.

How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:17:46+00:00Added an answer on June 6, 2026 at 5:17 pm

    You have to check whether the leaf directory exists, using stat(), and iteratively create directories in a top-down manner, using mkdir(). Or you may even abandon the check and start with a directory creation – it will just fail with bunch of EEXIST errors if your directories are already there. This is just an easy way.

    If you’re concerned about correctness, there is a better way. You should use openat() on subsequent available components (directories) of the desired path and mkdirat() to create lacking ones. That way you can avoid possible races. In the end, you should open your file using openat() too.

    Check relevant manpages to see how you should use mentioned system calls.


    If the above rod was not sufficiently long enough for you, then below you can find my simple Linux-flavored fish. xopen() function allows you to open the file in a given path, creating directories along the way.

    #include <errno.h>
    #include <fcntl.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    /* Look out! It modifies pathname! */
    int xopen(char *pathname, int flags, mode_t mode, mode_t dirmode)
    {
        int fd, errsv;
        int dirfd = AT_FDCWD;
        char *ptr = pathname;
    
        while (*ptr == '/')
            ptr++;
        for (;;) {
            strsep(&ptr, "/");
            if (ptr == NULL) {
                fd = openat(dirfd, pathname, flags, mode);
                break;
            }
            while (*ptr == '/')
                ptr++;
            if (*ptr == '\0') {
                errno = EISDIR;
                fd = -1;
                break;
            }
            if ((fd = mkdirat(dirfd, pathname, dirmode)) < 0 && errno != EEXIST)
                break;
            if ((fd = openat(dirfd, pathname, O_DIRECTORY)) < 0)
                break;
            if (dirfd != AT_FDCWD)
                close(dirfd);
            dirfd = fd;
            pathname = ptr;
        }
        errsv = errno;
        if (dirfd != AT_FDCWD)
            close(dirfd);
        errno = errsv;
    
        return fd;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write some radian angles to a binary file. Is there any
In my project, there is a need to read and write to binary file,
I need to write some input data files for a python program, and I
I need to write to a text file using JavaScript. I have a machine
To read/write binary files, I am using DataInputStream/DataOutputStream, they have this method writeByte()/readByte(), but
I want to write two simple utilities: Receives a Binary file, and converts it
I have various binary file formats which I need to dump to some kind
I trying to write to file in binary code. In mathematica i have some
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write a script in Matlab, which will read some data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.