Automatically generate file names

F

Freddy

Hi all,

I use a C program to generate lots of data while changing many
parameters sometimes.

However, I am trying to find a way to let C generate the output file
names dependent on a few parameters that I keep changing. this way it
saves the pain of having to back up the output file and rename it
before running the program again.

I don't know if there is a solution for that.


Freddy
 
C

Clever Monkey

Freddy said:
I use a C program to generate lots of data while changing many
parameters sometimes.

However, I am trying to find a way to let C generate the output file
names dependent on a few parameters that I keep changing. this way it
saves the pain of having to back up the output file and rename it
before running the program again.

I don't know if there is a solution for that.
tmpnam() may get you pretty close. You could munge the results to
retain uniqueness while providing dependencies upon your naming scheme.
 
M

Mike Wahler

Freddy said:
Hi all,

I use a C program to generate lots of data while changing many
parameters sometimes.

However, I am trying to find a way to let C generate the output file
names dependent on a few parameters that I keep changing. this way it
saves the pain of having to back up the output file and rename it
before running the program again.

I don't know if there is a solution for that.

Here's a simple function that creates a file
name which uses the parameter 'num':

#include <stdio.h>
#define FN_MAX_LEN 128 /* you need to determine best value for this */

const char *filename(unsigned int num)
{
static char filename[FN_MAX_LEN];
sprintf(filename, "%s%d", "file", num);
return filename;
}

int main()
{
unsigned int i = 0;
for(i = 1; i <= 5; ++i)
{
const char *fn = filename(i);
puts(fn);
}

return 0;
}

Output:

file1
file2
file3
file4
file5


-Mike
 
M

Malcolm McLean

Freddy said:
Hi all,

I use a C program to generate lots of data while changing many
parameters sometimes.

However, I am trying to find a way to let C generate the output file
names dependent on a few parameters that I keep changing. this way it
saves the pain of having to back up the output file and rename it
before running the program again.

I don't know if there is a solution for that.
sprintf(path, "myfile%sversion%galgo%s.freddy\n", programname, version,
algorithmname);

fp = fopen(path, "w");
 
J

Jack Klein

Here's a simple function that creates a file
name which uses the parameter 'num':

#include <stdio.h>
#define FN_MAX_LEN 128 /* you need to determine best value for this */

[snip]

If you use the standard macro FILENAME_MAX from the already-included
<stdio.h>, the determination is done for you.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
M

Mike Wahler

Jack Klein said:
On Mon, 26 Mar 2007 18:14:52 GMT, "Mike Wahler"
#include <stdio.h>
#define FN_MAX_LEN 128 /* you need to determine best value for this */

[snip]

If you use the standard macro FILENAME_MAX from the already-included
<stdio.h>, the determination is done for you.

I always seem to forget about that one. Thanks.

-Mike
 
K

Keith Thompson

Mike Wahler said:
Jack Klein said:
On Mon, 26 Mar 2007 18:14:52 GMT, "Mike Wahler"
#include <stdio.h>
#define FN_MAX_LEN 128 /* you need to determine best value for this */

[snip]

If you use the standard macro FILENAME_MAX from the already-included
<stdio.h>, the determination is done for you.

I always seem to forget about that one. Thanks.

FILENAME_MAX is not necessarily as meaningful as you might expect it
to be. The standard says it:

expands to an integer constant expression that is the size needed
for an array of char large enough to hold the longest file name
string that the implementation guarantees can be opened

with a footnote:

If the implementation imposes no practical limit on the length of
file name strings, the value of FILENAME_MAX should instead be the
recommended size of an array intended to hold a file name
string. Of course, file name string contents are subject to other
system-specific constraints; therefore all possible strings of
length FILENAME_MAX cannot be expected to be opened successfully.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top