By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

file name with variables

P: n/a
Hi,

I want to have variables in the name of the file created. like if i=3,
then I would like the 3rd file to have the name "file3". How do I do that
using fopen statement?

FILE *fp;
fp=fopen("file.....", "w");

Thanks,
Yulin
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Yulin Chang <yc****@artsci.wustl.edu> writes:
I want to have variables in the name of the file created. like if i=3,
then I would like the 3rd file to have the name "file3". How do I do that
using fopen statement?


Construct the name of the file using string manipulation
functions; e.g.
sprintf(filename, "file%d", number);
then use fopen() with the constructed filename as an argument.

By the way, fopen() is a function, not a statement.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Nov 13 '05 #2

P: n/a
Yulin Chang <yc****@artsci.wustl.edu> wrote:
Hi, I want to have variables in the name of the file created. like if i=3,
then I would like the 3rd file to have the name "file3". How do I do that
using fopen statement? FILE *fp;
fp=fopen("file.....", "w");


You need to use 'sprintf' to create the filename.
Such as:

sprintf(buf, "file%d", i);

Alex
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.