file related

Discussion in 'C Programming' started by cool.qwerty@rediff.com, Oct 30, 2007.

  1. Guest

    how we create multiple file with diffrent name in c program?
     
    , Oct 30, 2007
    #1
    1. Advertising

  2. said:

    > how we create multiple file with diffrent name in c program?


    See K&R2 pp160-162

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Oct 30, 2007
    #2
    1. Advertising

  3. pete Guest

    wrote:
    >
    > how we create multiple file with diffrent name in c program?


    /* BEGIN new.c */

    #include <stdio.h>
    #include <stdlib.h>

    #define NFILES 3
    #define MAX_LINES_PER_FILE 15
    #define LU_RAND_SEED 0LU
    #define LU_RAND(S) ((S) * 69069 + 362437 & 0XFFFFFFFFLU)
    #define NMEMB(A) (sizeof (A) / sizeof *(A))

    void fremove(char (*fn)[L_tmpnam], size_t nmemb);

    int main(void)
    {
    long unsigned index, lu_seed, line;
    int rc;
    char fn[NFILES][L_tmpnam];
    FILE *fp;

    puts("/* BEGIN new.c output */\n");
    /*
    ** Open temporary input text files for writing.
    ** Close each file after filling with long unsigned values.
    */
    lu_seed = LU_RAND_SEED;
    for (index = 0; index != NMEMB(fn); ++index) {
    tmpnam(fn[index]);
    fp = fopen(fn[index], "w");
    if (fp == NULL) {
    fremove(fn, index);
    puts("fopen(fn[index], \"w\") == NULL");
    exit(EXIT_FAILURE);
    }
    printf("\"%s\" open for writing.\n", fn[index]);
    line = lu_seed % MAX_LINES_PER_FILE + 1;
    while (line-- != 0) {
    lu_seed = LU_RAND(lu_seed);
    fprintf(fp, "%lu\n", lu_seed);
    printf( "%lu\n", lu_seed);
    }
    fclose(fp);
    printf("\"%s\" closed.\n\n", fn[index]);
    }
    /*
    ** Open input text files for reading.
    ** Close each temp input file after reading.
    ** Remove temporary input files.
    */
    for (index = 0; index != NMEMB(fn); ++index) {
    fp = fopen(fn[index], "r");
    if (fp == NULL) {
    fremove(fn, NMEMB(fn));
    puts("fopen(fn[index], \"r\") == NULL");
    exit(EXIT_FAILURE);
    }
    printf("\"%s\" open for reading.\n", fn[index]);
    while ((rc = getc(fp)) != EOF) {
    putchar(rc);
    }
    fclose(fp);
    printf("\"%s\" closed.\n\n", fn[index]);
    }
    fremove(fn, NMEMB(fn));
    puts("\n/* END new.c output */");
    return 0;
    }

    void fremove(char (*fn)[L_tmpnam], size_t nmemb)
    {
    while (nmemb-- != 0) {
    if (remove(fn[nmemb]) == 0) {
    printf("\"%s\" removed.\n", fn[nmemb]);
    }
    }
    }

    /* END new.c */

    --
    pete
     
    pete, Oct 30, 2007
    #3
  4. santosh Guest

    wrote:

    > how we create multiple file with diffrent name in c program?


    Your question is not clear. There are many possibilities. One of them
    involves adding a variable prefix or suffix to a chosen name. Another
    possibility is to use tmpfile() function to open unique files.

    If you describe the exact context of your problem we may be able to
    suggest better solutions.
     
    santosh, Oct 30, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manoj Nair
    Replies:
    0
    Views:
    423
    Manoj Nair
    Jun 4, 2004
  2. Maxwell Hammer
    Replies:
    7
    Views:
    658
    Peter Hansen
    Jun 18, 2005
  3. Steven Nagy
    Replies:
    3
    Views:
    740
    Steven Nagy
    Feb 13, 2007
  4. Replies:
    3
    Views:
    374
    Keith Thompson
    Jun 22, 2005
  5. Replies:
    4
    Views:
    256
    Nikita the Spider
    Apr 15, 2007
Loading...

Share This Page