Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C file creation problem

Options
  • 06-05-2004 7:29pm
    #1
    Closed Accounts Posts: 28


    Hi,
    I'm trying to make a C program, but i'm having difficulty with a simple problem.
    I want to tell the computer to make a file name that I type in using the file structure.
    I can't do it, and i'm just wondering if it's even possible.

    here's my attempt:

    #include <stdio.h>

    main(){
    char strpr;
    FILE *fp;
    printf("Enter name:");
    scanf("%s", &flnme);

    fp=fopen("%s", flnme, "a");
    fprintf(fp, "it works");
    fclose(fp);
    }

    if anyone can help, that'd be great!


Comments

  • Registered Users Posts: 491 ✭✭Silent Bob


    What is strpr used for? Also where has flnme been declared?

    What compiler and platform are you using? I haven't seen a fopen call that takes three arguments, an ANSI C implementation only takes two arguments.

    Assuming that flnme is a character pointer you should be calling
    FILE* fp = fopen(flnme, "a");
    


  • Registered Users Posts: 4,183 ✭✭✭deadl0ck


    Yep - you never declared flnme, also, your call to fopen was wrong.

    This'll work for any input string smaller than 100 chars

    [PHP]
    #include <stdio.h>

    #define MAX_INPUT_LEN 100

    main()
    {
    FILE *fp = NULL;
    char flnme[MAX_INPUT_LEN];

    /* Init the flnme char array */
    memset(flnme, 0, sizeof(char) * MAX_INPUT_LEN);

    printf("Enter name:");
    scanf("%s", &flnme);
    fp = fopen(flnme, "a");
    fprintf(fp, "it works");
    fclose(fp);
    }
    [/PHP]


  • Closed Accounts Posts: 28 paniniter


    Thanks for your help. I've got it working now.


Advertisement