fopen

This is a discussion on fopen within the C Programming forums, part of the General Programming Boards category; I wrote a program which asks the user for certain input. The information that the users input are put into ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    17

    fopen

    I wrote a program which asks the user for certain input. The information that the users input are put into a couple of output files. The data in the output files get combined in various ways and a new output file is created.

    Because the program is going to be used for multiple users, it would be
    helpful if the output file could have a different name for each user. What I
    was hoping was that each user could enter their username. Then their output
    file would be their username.txt. As far as I understand, there is no way to
    include a variable name in the fopen command. If there were, the filename
    that needs to be included in the fopen command, could simply be their
    username + .txt.

    The only way that I can think of handling this, is by including the whole
    body of the program in multiple if statements, with the if statement being
    contingent on the particular user name. If the username is ______, do the
    whole program with the output file being XYZ. If the username is ________,
    do the whole program with the output file being XYZ1 etc. etc.

    Please advise. Many thanks!

    RF

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >As far as I understand, there is no way to include a variable name in the fopen command.
    Considering fopen()'s first argument is a variable, I don't see how that's the case...

    What's wrong with building the filename and passing it to fopen()?

    Code:
    char filename[256];
    
    strncpy(filename, "username", sizeof(filename));
    if((strlen(filename) - sizeof(filename)) >= 4)
    {
        strcat(filename, ".txt");
    }else{
        /* Username too long... or some other error ... */
    }
    
    fopen(filename, "w+");
    
    /* ... */

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    17
    Thanks zack. Your suggestion was great! I thought fopen could only accept literal strings as its first parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  2. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 05:27 AM
  3. problem with fopen command
    By emon in forum C Programming
    Replies: 2
    Last Post: 03-11-2004, 11:11 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21