Thread: Opening a user-defined file

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Opening a user-defined file

    Hi. This is probably a really simple question but I can't find how to do it in any of my notes!


    I am writing a program in C and part of it needs to create a file, the name of which is inputted by the user. I have a char string called filename, which the user chooses using scanf. I then need to create the file with "filename".xls and add data to it. My file pointer is called 'file'. I'm guessing I need to use a line something like:

    file=fopen("%s.xls",filename "w");

    or:

    file=fopen("(filename).xls","w");

    But neither of these work!

    Can anyone tell me what to do?

    Cheers.

    Chris

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    char *fullfilename = malloc(strlen(filename) + 5);
    if ( fullfilename == NULL )
      /* Do error handling here but make sure you don't continue until fullfilename is not null */
    
    strcpy (fullfilename, filename);
    strcat (fullfilename,".xls");
    file = fopen(fullfilename, "w");

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Thanks for that! It's working now

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    OK now this one really is a stupid question:

    How do I use pi in calculations?

    I've included math.h, and i've tried M_PI but it doesn't work


    Thanks again!


    Chris

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    but it doesn't work
    Post your code

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    impinduc=2*3.142*workingfreq*indctnce;


    I just want to use the exact value of pi instead of 3.142!

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by mr_diss
    I've included math.h, and i've tried M_PI but it doesn't work
    M_PI is nonstandard.
    Quote Originally Posted by mr_diss
    I just want to use the exact value of pi instead of 3.142!
    Try this.
    Code:
    impinduc = 2 * (4.0 * atan(1.0)) * workingfreq * indctnce;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Thanks that works a bit better, is there no constant for just pi?

    I presume that's using the inverse tan of 1 or something is it?

  9. #9
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    I would define it myself so:

    Code:
    #define PI 3.1415926536     /* as many digits as I can remember */
    or, following Dave's suggestion (I quite like that)
    Code:
    #define PI (4.0 * atan(1.0))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. User Defined save file (help)
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 04-16-2002, 11:13 PM