Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, I wanted to created file names dynamically, so i tried something like this, fun(){ int dy=97; while(cond){ --- ---- name[0]='n';name[1]='m';name[2]='a'; name[3]=dy; //so i got nmaa open (file) //do ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Thumbs up How to dynamically create file names?

    Hi all,
    I wanted to created file names dynamically,
    so i tried something like this,
    fun(){
    int dy=97;
    while(cond){
    ---
    ----
    name[0]='n';name[1]='m';name[2]='a';
    name[3]=dy;
    //so i got nmaa
    open (file)
    //do something
    close(file)
    }
    ---
    ---
    but the problem is i can created file names like,
    nmaa,nmab,nmac,namd.........namz.
    only upto z...
    how to dynamically create unique file name?
    If you know some other logic or solution please help me.
    Thanks.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    one possible option for you - read:

    Code:
    man mktemp

  3. #3
    Just Joined!
    Join Date
    Jan 2007
    Posts
    23

    creating files....

    a unique filename can be generated by tmpnam() function.
    if the temporary file is to be used immediately you can name it and open it
    uing tmpfile() function.


    #include<stdio.h>


    int main()
    {
    char tmpname[L_tmpnam];
    char *filename;
    FILE *tmpfp;

    filename=tmpnam(tmpname);

    printf("Temporary file name is:%s\n",filename);
    tmpfp=tmpfile();
    if(tmpfp)
    printf("Opened a temporary file OK\n");
    else
    perror("Tmpfile");
    exit(0);
    }

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    Thanks guyz...i will try both and let you know result.
    Thanks u
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  5. #5
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    I'm sorry -- I was thinking 'bash shell', instead of 'C'... (guess I wasn't paying attention...)

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    Hi,
    in C too we have mktemp see here
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...