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

append output filename

P: n/a
vj
I am using Borland C++.

I run a program which generates output data files, say 'Voltage.dat',
'Current.dat'. I have a variable in my code (say N), and for various
values of N (for each value of N), I generate these output data files.
After running the program once for a given value of N (say N equal to
1) and when the programme is finished, I manually append the generated
filename with the value of N, such as 'Voltage_N1.dat',
'Current_N1.dat'. Then I go back to the code, make N=2, repeat the
procedure and append the generated output files with '_N2' etc.

Is there a simple way of appending the filenames automatically? In
other words, I want to run the programme in a loop for various values
of N (I know this bit i.e. how to make a loop:), and for each run, the
files generated (such as 'Voltage_N ') is appended by the value of N
i.e. 1, 2 and so on, followed by the extension '.dat'.
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in C++.
(An easy to understand and basic reply with steps will be highly
appreciated).

Thankyou

May 24 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a

vj wrote:
I am using Borland C++.

I run a program which generates output data files, say 'Voltage.dat',
'Current.dat'. I have a variable in my code (say N), and for various
values of N (for each value of N), I generate these output data files.
After running the program once for a given value of N (say N equal to
1) and when the programme is finished, I manually append the generated
filename with the value of N, such as 'Voltage_N1.dat',
'Current_N1.dat'. Then I go back to the code, make N=2, repeat the
procedure and append the generated output files with '_N2' etc.

Is there a simple way of appending the filenames automatically? In
other words, I want to run the programme in a loop for various values
of N (I know this bit i.e. how to make a loop:), and for each run, the
files generated (such as 'Voltage_N ') is appended by the value of N
i.e. 1, 2 and so on, followed by the extension '.dat'.
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in C++.
(An easy to understand and basic reply with steps will be highly
appreciated).

Thankyou
May 24 '07 #2

P: n/a

vj wrote:
I am using Borland C++.

I run a program which generates output data files, say 'Voltage.dat',
'Current.dat'. I have a variable in my code (say N), and for various
values of N (for each value of N), I generate these output data files.
After running the program once for a given value of N (say N equal to
1) and when the programme is finished, I manually append the generated
filename with the value of N, such as 'Voltage_N1.dat',
'Current_N1.dat'. Then I go back to the code, make N=2, repeat the
procedure and append the generated output files with '_N2' etc.

Is there a simple way of appending the filenames automatically? In
other words, I want to run the programme in a loop for various values
of N (I know this bit i.e. how to make a loop:), and for each run, the
files generated (such as 'Voltage_N ') is appended by the value of N
i.e. 1, 2 and so on, followed by the extension '.dat'.
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in C++.
(An easy to understand and basic reply with steps will be highly
appreciated).

Thankyou
May 24 '07 #3

P: n/a
Hi,

Try this....

FILE fp1;
char filename[30];

for(int i=0;i<10;i++)
{
......
......

sprintf(filename,"Voltage_N%d",i);
fp1 = fopen(filename,"w");

//Do all the 'Writing to the File' here..

fclose(fp1);
}

I guess this wud solve ur problem...

On May 24, 10:05 am, vj <vijayjany...@gmail.comwrote:
I am using Borland C++.

I run a program which generates output data files, say 'Voltage.dat',
'Current.dat'. I have a variable in my code (say N), and for various
values of N (for each value of N), I generate these output data files.
After running the program once for a given value of N (say N equal to
1) and when the programme is finished, I manually append the generated
filename with the value of N, such as 'Voltage_N1.dat',
'Current_N1.dat'. Then I go back to the code, make N=2, repeat the
procedure and append the generated output files with '_N2' etc.

Is there a simple way of appending the filenames automatically? In
other words, I want to run the programme in a loop for various values
of N (I know this bit i.e. how to make a loop:), and for each run, the
files generated (such as 'Voltage_N ') is appended by the value of N
i.e. 1, 2 and so on, followed by the extension '.dat'.
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in C++.
(An easy to understand and basic reply with steps will be highly
appreciated).

Thankyou

May 24 '07 #4

P: n/a
SquareoftheHypotenuse said:
Hi,

Try this....

FILE fp1;
Not what you meant.
char filename[30];

for(int i=0;i<10;i++)
{
.....
.....

sprintf(filename,"Voltage_N%d",i);
fp1 = fopen(filename,"w");

//Do all the 'Writing to the File' here..
Not till you checked that the fopen succeeded.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 24 '07 #5

P: n/a
vj
On May 24, 11:49 am, SquareoftheHypotenuse
<squareofthehypoten...@gmail.comwrote:
Hi,

Try this....

FILE fp1;
char filename[30];

for(int i=0;i<10;i++)
{
.....
.....

sprintf(filename,"Voltage_N%d",i);
fp1 = fopen(filename,"w");

//Do all the 'Writing to the File' here..

fclose(fp1);

}

I guess this wud solve ur problem...

On May 24, 10:05 am, vj <vijayjany...@gmail.comwrote:
I am using Borland C++.
I run a program which generates output data files, say 'Voltage.dat',
'Current.dat'. I have a variable in my code (say N), and for various
values of N (for each value of N), I generate these output data files.
After running the program once for a given value of N (say N equal to
1) and when the programme is finished, I manually append the generated
filename with the value of N, such as 'Voltage_N1.dat',
'Current_N1.dat'. Then I go back to the code, make N=2, repeat the
procedure and append the generated output files with '_N2' etc.
Is there a simple way of appending the filenames automatically? In
other words, I want to run the programme in a loop for various values
of N (I know this bit i.e. how to make a loop:), and for each run, the
files generated (such as 'Voltage_N ') is appended by the value of N
i.e. 1, 2 and so on, followed by the extension '.dat'.
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in C++.
(An easy to understand and basic reply with steps will be highly
appreciated).
Thankyou- Hide quoted text -

- Show quoted text -
Hi SquareoftheHypotenuse
Thanks a lot to you (and all others who have shown interest) for a
nice reply.
It works amazingly, only thing is that initially I got an error, and
as prompted by the error message, I rectified it by using "FILE *fp1;"
instead of "FILE fp1;", and I am sure this is what you meant :)
For the time being, I just tested this bit of code to see if the
desired output files are generated (without actually writing any data
into those files). And the output files are generated as desired !
Could you please tell me what function (and its format) should I use
to write data to files (i.e. fprintf?). Because I normally use a
statement like "filename<<data1<<data2<<endl;"
Thankyou

May 24 '07 #6

P: n/a
"vj" wrote
SquareoftheHypotenuse wrote:
>Try this....

FILE fp1;
char filename[30];

for(int i=0;i<10;i++)
{
.....
.....

sprintf(filename,"Voltage_N%d",i);
fp1 = fopen(filename,"w");

//Do all the 'Writing to the File' here..

fclose(fp1);

}
It works amazingly, only thing is that initially I got an error, and
as prompted by the error message, I rectified it by using "FILE *fp1;"
instead of "FILE fp1;", and I am sure this is what you meant :)
For the time being, I just tested this bit of code to see if the
desired output files are generated (without actually writing any data
into those files). And the output files are generated as desired !
Could you please tell me what function (and its format) should I use
to write data to files (i.e. fprintf?). Because I normally use a
statement like "filename<<data1<<data2<<endl;"
Here's my go:

#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int ac, char** av)
{
int n = 1;
int data1 = 14;
double data2 = 3.0;

if(ac 1)
n = strtol(av[1],NULL,10);

for(int i=0; i<n; ++i)
{

ostringstream oss;
oss << "Filename_N" << i + 1;
string fname(oss.str());
ofstream fout(fname.c_str());

if(fout)
{
fout << data1 << data2 << endl;
}
}

return 0;
}

--
Randy

May 24 '07 #7

P: n/a
On May 24, 7:05 am, vj <vijayjany...@gmail.comwrote:
I run a program which generates output data files, say 'Voltage.dat',
'Current.dat'. I have a variable in my code (say N), and for various
values of N (for each value of N), I generate these output data files.
After running the program once for a given value of N (say N equal to
1) and when the programme is finished, I manually append the generated
filename with the value of N, such as 'Voltage_N1.dat',
'Current_N1.dat'. Then I go back to the code, make N=2, repeat the
procedure and append the generated output files with '_N2' etc.
Is there a simple way of appending the filenames automatically? In
other words, I want to run the programme in a loop for various values
of N (I know this bit i.e. how to make a loop:), and for each run, the
files generated (such as 'Voltage_N ') is appended by the value of N
i.e. 1, 2 and so on, followed by the extension '.dat'.
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in C++.
(An easy to understand and basic reply with steps will be highly
appreciated).
std::ostringstream filename ;
filename << "Voltage_" << N << ".dat" ;
std::ofstream file( filename.str().c_str() ) ;

Avoid the functions strcpy and strcat; they are not very safe.
If you are a beginner, it's much, much easier to use
std::string (and std::vector, instead of T[]). The iostream
functions are a bit more complicated, but still a great deal
easier and safer to use than the FILE* mess inherited from C.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #8

P: n/a
James Kanze said:
On May 24, 7:05 am, vj <vijayjany...@gmail.comwrote:
<snip>
>I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in
C++.
<snip>
Avoid the functions strcpy and strcat; they are not very safe.
In C, strcpy and strcat are both perfectly safe if used correctly. Do
the C++ versions of these functions have different semantics or
interfaces? </rhetorical>
If you are a beginner, it's much, much easier to use
std::string (and std::vector, instead of T[]). The iostream
functions are a bit more complicated, but still a great deal
easier and safer to use than the FILE* mess inherited from C.
I can't agree that iostreams are easier to use than FILE *. I believe
that you believe it, but it is certainly a matter of opinion.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 25 '07 #9

P: n/a
On May 25, 9:40 am, Richard Heathfield <r...@see.sig.invalidwrote:
James Kanze said:
On May 24, 7:05 am, vj <vijayjany...@gmail.comwrote:
<snip>
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in
C++.
<snip>
Avoid the functions strcpy and strcat; they are not very safe.
In C, strcpy and strcat are both perfectly safe if used correctly.
Everything's perfectly safe "if used correctly". The question
is how difficult it is to use them correctly. Some functions,
like gets or sprintf, are impossible, or almost impossible to
use correctly. strcpy and strcat, while not as bad, require a
fair bit of care as well (and the C standard's committee has
proposed replacements, which are less dangerous). std::string
just works. Why use something that requires considerable care
and attention, when there is a simple solution which just works
available?
Do
the C++ versions of these functions have different semantics or
interfaces? </rhetorical>
C++ supports the C interfaces for reasons of C compatibility.
In C++, however, the type of a string is std::string, not
char[], and the functionality is supported with the = and the +=
operators. The result is something that is both easier to
write, and safer.
If you are a beginner, it's much, much easier to use
std::string (and std::vector, instead of T[]). The iostream
functions are a bit more complicated, but still a great deal
easier and safer to use than the FILE* mess inherited from C.
I can't agree that iostreams are easier to use than FILE *. I believe
that you believe it, but it is certainly a matter of opinion.
If the goal is a core dump, then printf is definitely easier
than anything in iostreams. But for anything useful, iostream's
beats FILE* hands down. How do you output a user defined type
using printf? How do you output to a socket? How do you read
skipping comments, or write putting a time stamp at the
beginning of each line?

In the end, FILE* isn't really even usable.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #10

P: n/a
James Kanze said:
On May 25, 9:40 am, Richard Heathfield <r...@see.sig.invalidwrote:
>James Kanze said:
<snip>
Avoid the functions strcpy and strcat; they are not very safe.
>In C, strcpy and strcat are both perfectly safe if used correctly.

Everything's perfectly safe "if used correctly".
Well, gets() isn't (unless you count 'not used' as the only possible
example of 'used correctly').
The question is how difficult it is to use them correctly.
In the case of strcpy and strcat, it isn't terribly difficult.
Some functions,
like gets or sprintf, are impossible, or almost impossible to
use correctly.
sprintf is fairly easy to use correctly.
strcpy and strcat, while not as bad, require a
fair bit of care as well
Sure. Programming /does/ require care, and using API functions properly
is part of that care. Likewise, the STL requires a fair bit of care to
use correctly.
(and the C standard's committee has
proposed replacements, which are less dangerous).
I disagree. I will, however, accept that they have proposed replacements
which /they/ consider less dangerous.
std::string just works.
If used correctly, yes. If used incorrectly, however, it doesn't.
Why use something that requires considerable care
and attention, when there is a simple solution which just works
available?
For the same reason(s) that some people prefer manual transmission
gearboxes, even though it means they have to use the pedal (and indeed
use it correctly).
>Do
the C++ versions of these functions have different semantics or
interfaces? </rhetorical>

C++ supports the C interfaces for reasons of C compatibility.
Yes, because C compatibility is *important* to C++.
In C++, however, the type of a string is std::string, not
char[], and the functionality is supported with the = and the +=
operators. The result is something that is both easier to
write, and safer.
How can it be safer than "perfectly safe if used correctly"? And if it's
used incorrectly, clearly it is no safer than an incorrectly-used
strcpy.
If you are a beginner, it's much, much easier to use
std::string (and std::vector, instead of T[]). The iostream
functions are a bit more complicated, but still a great deal
easier and safer to use than the FILE* mess inherited from C.
>I can't agree that iostreams are easier to use than FILE *. I believe
that you believe it, but it is certainly a matter of opinion.

If the goal is a core dump, then printf is definitely easier
than anything in iostreams.
For you, perhaps, but not for me. It is a matter of opinion.
But for anything useful, iostream's
beats FILE* hands down. How do you output a user defined type
using printf?
By writing a user-defined-type-specific function to do it, same as you
do in C++. Syntactic sugar doesn't mask this fact.
How do you output to a socket?
inetd
How do you read
skipping comments, or write putting a time stamp at the
beginning of each line?
Well, I use a line-capture routine of my own to read lines from a text
stream. If the stream I'm reading contains comments, the way in which I
skip them will depend on the comment format. For any comment format you
can already handle, I can come up with a dozen or more that you can't.
To add support for these, you'd have to cut code, whether you're using
iostreams or FILE *. As for putting a time stamp at the beginning of
each line, printf can handle that easily, when used in conjunction with
a little routine for formatting the time as a string in the format you
want. And, for every time format that you already support, I can easily
come up with a dozen that you don't. To add support for these, you have
to cut code, same as me.
>
In the end, FILE* isn't really even usable.
You're absolutely right, which is why it's so strange that I manage to
get excellent results with it. Perhaps I'm a genius. Do you think that
could be it? Or do you perhaps think it more likely that FILE * isn't
as hard to use as you make out?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 25 '07 #11

P: n/a
"vj" <vi**********@gmail.comwrote in message
news:11**********************@a26g2000pre.googlegr oups.com...
>I am using Borland C++.

I run a program which generates output data files, say 'Voltage.dat',
'Current.dat'. I have a variable in my code (say N), and for various
values of N (for each value of N), I generate these output data files.
After running the program once for a given value of N (say N equal to
1) and when the programme is finished, I manually append the generated
filename with the value of N, such as 'Voltage_N1.dat',
'Current_N1.dat'. Then I go back to the code, make N=2, repeat the
procedure and append the generated output files with '_N2' etc.

Is there a simple way of appending the filenames automatically? In
other words, I want to run the programme in a loop for various values
of N (I know this bit i.e. how to make a loop:), and for each run, the
files generated (such as 'Voltage_N ') is appended by the value of N
i.e. 1, 2 and so on, followed by the extension '.dat'.
I know after reading the help file that there are functions like
strcpy and strcat, but couldn't figure out how to use them and their
exact format. Also, I am not conversant with Classes, pointers in C++.
(An easy to understand and basic reply with steps will be highly
appreciated).
You didn't specify for C or C++.

C (I'm a little rusty in C:
char FileName[100];
FileName[0] = '\0';
for ( int i = 0; i < 10; ++i )
{
strcat( FileName, "Voltage_N" );
strcat( FileName, itoa( i ) );
strcat( FileName, ".dat" );
// At this point FileName should contain "Voltage_N0.dat",
"Voltage_N1.dat", etc...

// Call function passing FileName or use here.
}

C++:
for ( int i = 0; i < 10; ++i )
{
std::stringstream FileName;
FileName << "Voltage_N" << i << ".dat";
// at this point FileName.str() contains "Voltage_N0.dat",
"Voltage_N1.dat", etc...
}

There are many other ways to do it. itoa converts an interger to a c-style
string. I'm not positive it's available on all distrubutions (dont' know if
it's standard). If it doesnt' work for you, find a function/method/whatver
that does work for you. The C++ way is standard.
May 26 '07 #12

P: n/a
Jim Langston said:
"vj" <vi**********@gmail.comwrote in message
news:11**********************@a26g2000pre.googlegr oups.com...
>>I am using Borland C++.
<snip>
>exact format. Also, I am not conversant with Classes, pointers in
C++.
<snip>
You didn't specify for C or C++.
He does kind of imply C++, though, both by his words and his choice of
crossposted group.
C (I'm a little rusty in C:
So I see. :-)

I won't try to correct yours, since you're probably not all that fussed
anyway, but for the OP's benefit, here's a simple, correct, C fragment:

char FileName[FILENAME_MAX] = {0};
int i = 0;

while(i < 10)
{
sprintf(FileName, "Voltage_N%d.dat", i++);
C++:
for ( int i = 0; i < 10; ++i )
{
std::stringstream FileName;
FileName << "Voltage_N" << i << ".dat";
// at this point FileName.str() contains "Voltage_N0.dat",
"Voltage_N1.dat", etc...
}

There are many other ways to do it. itoa converts an interger to a
c-style string.
That isn't guaranteed.
I'm not positive it's available on all distrubutions
It isn't.
(dont' know if it's standard).
It isn't.
If it doesnt' work for you, find a
function/method/whatver
that does work for you.
See above.
The C++ way is standard.
So is the C way (above).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 26 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.