create unique file

This is a discussion on create unique file within the C Programming forums, part of the General Programming Boards category; What I'm trying to do - if possible is open(create) a file using a dynamicly generated file name. I"m working ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    15

    create unique file

    What I'm trying to do - if possible is open(create) a file using a dynamicly generated file name.



    I"m working in a Linux environment using C



    Currently the existing function is passed four variables from the main program, the function creates a message using the variables, then writes it to a file. One of the variables passed to the function is a "message ID" it would be perfect if I could find a way to name the file to be created with the messageID variable.



    In theory Something like:

    FILE *newfile;

    newfile = fopen("%s",msgID, "w");



    would be great, but of course fopen won't accept the additional argument so that won't work.



    In any case, if I could just create a new unique file for each instance I could work around it not having the exact message ID as it's name.



    Any ideas would be appreciated.



    rm

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Use sprintf to create whatever name you like, in whatever format you like, then open that

    char fname[100];
    sprintf( fname, ....
    fp = fopen( fname, ...

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    15
    Originally posted by Salem
    Use sprintf to create whatever name you like, in whatever format you like, then open that



    char fname[100];

    sprintf( fname, ....

    fp = fopen( fname, ...
    Either I'm misunderstanding (quite possible) or this doesn't do what I want.
    I understand that I can create whatever file name I want and open it, but I would like a new file name created at every time the function is called.

    The function looks like this:

    int bounce(char *Uname, char *Udomain, char *Sname, char *msgID)
    //the above variables are passed to the function from the main program

    FILE = *newfile
    newfile=fopen("bounce","w")'; //This creates a new file named bounce
    //but it is overwritten when the next
    //variables are passed.
    //the function continues with:
    fprintf(newfile,"Bla Bla%sbla bla%s bla%s",Uname, Udoman.....);
    fclose(newfile);

    The idea is to have a new file created, ideally using the *msgID variable as the file name, thereby generating a new file with each instance.

    The problem is that fopen won't accept a variable argument for the file name - only a constant within the " ..". Also it won't accept a variable without giving a warning and then dumping core if I try to run it.

    Again, I could be missing something - I'm very new at this - type slowly!

    thanks

    rm

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Given that msgID is already a char*, I'm guessing it is also a string (one containing printable chars, ending in \0 )

    If this is so, then it's dead easy

    FILE *newfile;
    newfile=fopen(msgID,"w");

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    15
    Originally posted by Salem
    Given that msgID is already a char*, I'm guessing it is also a string (one containing printable chars, ending in \0 )



    If this is so, then it's dead easy



    FILE *newfile;

    newfile=fopen(msgID,"w");
    Hey, thanks for the quick reply....

    You guessed right it is a string with printable characters ending in \0

    but when I use:
    FILE *newfile;
    newfile=fopen(msgID,"w"); [/B][/QUOTE]

    The compiler warns:: Dammit

    Forget all that , the compiler warning was about a mistake in my fprintf statement. Damn - I was sure I tried it this way berfore!

    Thanks it works great.!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    int main( void )
    {
        char buf[80]={0};
        FILE *fp = NULL;
    
        printf("Enter a  name: ");
        fscanf( buf, 80, stdin );
    
        fp = fopen( buf, "w" );
        fputs( buf, fp );
        fclose( fp );
    
         return 0;
    }
    Now then, assuming you enter a valid name, this program will open a file for writing with the name you provide, and write that name to the file, then it will close the file.

    Do you understand now?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM

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