Thread: File names using a user defined name

  1. #1
    Registered User
    Join Date
    Apr 2011

    File names using a user defined name

    I'm a beginner in C and i wrote a function that is supposed to save information to a file, however the file should have different user defined names for multiple users. I asked the user to input their first name before i call the function and then use that first name to name the file. However, as much as I've looked I cannot find it in my notes and nothing I've tried has worked. this is what i have so far

    void dude(char first[10], float mark1, float mark2, float mark3, float mark4, float mark5, float mark6, float mark7, float final)
    FILE *cfPtr;

    cfPtr = fcreate("firstname.txt", "w");//i want the file name to be a first name
    fprintf(cfPtr, "%s\n\n", first);//this is the user's name
    fprintf(cfPtr, "Mark 1 is: %.2f\n", mark1);
    fprintf(cfPtr, "Mark 2 is: %.2f\n", mark2);
    fprintf(cfPtr, "Mark 3 is: %.2f\n", mark3);
    fprintf(cfPtr, "Mark 4 is: %.2f\n", mark4);
    fprintf(cfPtr, "Mark 5 is: %.2f\n", mark5);
    fprintf(cfPtr, "Mark 6 is: %.2f\n", mark6);
    fprintf(cfPtr, "Mark 7 is: %.2f\n", mark7);
    fprintf(cfPtr, "Final is: %.2f\n", final);


  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Melbourne, Australia
    I'm guessing first[] is the first name of the person. Have you tried something like
    cfPtr = fcreate( first, "w" );

    The file input/output functions work just as well using string variables for the filename as strings enclosed in double quotes.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Oh i figured out how to do it i had to make use of sprintf so i could use variable names as a filename. Thanks
    Last edited by KnightMan; 04-11-2011 at 10:25 PM. Reason: figured it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to extract Defined Function Names from C file
    By imranhabib in forum C Programming
    Replies: 11
    Last Post: 01-09-2008, 12:06 AM
  2. user defined file I/O
    By montablac in forum C++ Programming
    Replies: 3
    Last Post: 06-05-2005, 04:26 AM
  3. Opening a user-defined file
    By mr_diss in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 03:47 PM
  4. User Defined save file (help)
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 04-16-2002, 11:13 PM

Tags for this Thread