file related

P

pete

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 */
 
S

santosh

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top