Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C file creation problem

  • 06-05-2004 06: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, Registered Users 2 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, Registered Users 2 Posts: 4,127 ✭✭✭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