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 8932229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:17:27+00:00 2026-06-15T09:17:27+00:00

I want to create a file with name that input by user. Here is

  • 0

I want to create a file with name that input by user.

Here is my code:

int function1(){

    char path[50];
    char choice_f;
    char choice_m;
    int flag;     <-- updated
    mode_t mode;

    printf("Filename : ");
    scanf("%s", path);
    //printf("%s", path);     <-- path can be shown
    printf("\n");

    printf("Desire flag : ");
    scanf("%c", &choice_f);                 <-- updated
    //printf("%s", path);     <-- path CANNOT be shown from here
    while (choice_f != '1' && choice_f != '2' && choice_f != '3'){
        printf("Invalid input! Flag: ");
        scanf("%c", &choice_f);             <-- updated
    }
    if (choice_f == '1')
        flag = O_RDONLY;                      <-- updated
    else if (choice_f == '2')
        flag = O_WRONLY;                      <-- updated
    else if (choice_f == '3')
        flag = O_RDWR;                    <-- updated

    printf("Desire mode : ");
    scanf("%c", &choice_m);                <-- updated
    while(choice_m != '1' && choice_m != '2' && choice_m != '3' && choice_m != '4'){
        printf("Invalid input! Mode: ");
        scanf("%c", &choice_m);                <-- updated
    }
    if (choice_m == '1')
        mode = S_IRWXU;
    else if (choice_m == '2')
        mode = S_IRWXG;
    else if (choice_m == '3')
        mode = S_IRWXO;
    else if (choice_m == '4')
        mode = S_IRWXU | S_IRWXG | S_IRWXO;


    int fd = open(path, (int)flag|O_EXCL|O_CREAT);
    fchmod(fd, mode);
    printf("%d", fd);
    if (fd == -1){
        perror("error");
        return 1;
    }else
        printf("File \'%s\' Created \n", path);
    if (close(fd)<0)
        perror("close()");
    return 0;
}

But at the end of the program, I get the error message:

Error: Bad file descriptor

I tested the path with several times, and seems like after the second scanf(), the path cannot be shown. Even I tried to assign the path to another variable, it’s still the same.

What should I do to solve this error?

  • 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-15T09:17:27+00:00Added an answer on June 15, 2026 at 9:17 am

    I’m not sure if this is the only problem but you’re setting of choice_f and choice_m is incorrect and should be

    scanf("%c", &choice_f);
    scanf("%c", &choice_m);
    

    instead. i.e. You should use the %c format specifier instead. If you use %s, a nul terminator (at the very least) will be written; you haven’t provided storage for this so other stack variables will be over-written.

    After that, as suggested by Nicholas Wilson, you should change flags to be int rather than char* (and remove the quotes from the assignments to O_RDONLY etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a file with a block of code in it and
I want to create log file for my flex application. That is the file
Hi I have a simple form that allows a user to input a name,
I want to scan a folder whose path is defined by user input &
I want to create user profile with image. That user can upload his photo,
I'm writing a program that gathers user input for two file names, and opens
I'm creating a Magento module that saves user-input data to an xml file (held
I have here this code. I want it to first display an image file
I want to create a file in the current directory (where the executable is
Possible Duplicate: PHP create a file without fopen I want to create a file

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.