473,425 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Automatically generate file names

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

Mar 26 '07 #1
6 2692
Freddy wrote:
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.
Mar 26 '07 #2

"Freddy" <zf********@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
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

Mar 26 '07 #3

"Freddy" <zf********@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
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");

Mar 26 '07 #4
On Mon, 26 Mar 2007 18:14:52 GMT, "Mike Wahler"
<mk******@mkwahler.netwrote in comp.lang.c:
>
"Freddy" <zf********@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
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 */
[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
Mar 27 '07 #5

"Jack Klein" <ja*******@spamcop.netwrote in message
news:g5********************************@4ax.com...
On Mon, 26 Mar 2007 18:14:52 GMT, "Mike Wahler"
<mk******@mkwahler.netwrote in comp.lang.c:
>#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
Mar 27 '07 #6
"Mike Wahler" <mk******@mkwahler.netwrites:
"Jack Klein" <ja*******@spamcop.netwrote in message
news:g5********************************@4ax.com...
>On Mon, 26 Mar 2007 18:14:52 GMT, "Mike Wahler"
<mk******@mkwahler.netwrote in comp.lang.c:
>>#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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 27 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: HD | last post by:
Hello. For the following, I would appreciate if anyone could tell me: if it can be done, how it might done, and/or what search terms I could use to find the solution myself. I would like to...
2
by: Shaker | last post by:
Hello, I would like to write a program that would automatically take out the spaces in directory names and file names . I would like this because my web server does not allow spaces in names. I...
37
by: nobody | last post by:
I am writing a framework that other developers will write plug-ins for. I would like for one of the features of the framework to be to intercept all text written to stdout/stderr and prepend...
4
by: WB | last post by:
Hi, How can I generate web controls such as textboxes and drop-menus on the fly? My web application allows users to fill out PDF forms online. There are many PDF forms, and my application...
1
by: needin4mation | last post by:
I want to use the Menu and SiteMapPath. But it looks like I have the create the xml file that stores the data by hand. What if I had a site with 500 pages? That seems to be to cumbersome. Is...
1
by: comp.lang.php | last post by:
Consider my code: /** * Set the TTF array property $ttfArray * * @access private * @see actual_path */ function &setTTFArray() { // STATIC VOID METHOD
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
23
by: Geoff Cox | last post by:
Hello, Is it possible to use php to generate different menus for users who have access to files in different folders? For example, user Fred might be able to access files in folders A, B and...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.