append output filename

V

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
 
T

tasneem.alsir

vj said:
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
 
T

tasneem.alsir

vj said:
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
 
S

SquareoftheHypotenuse

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...
 
R

Richard Heathfield

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.
 
V

vj

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...

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
 
R

R. Scott Mellow

SquareoftheHypotenuse said:
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;
}
 
J

James Kanze

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::eek:stringstream filename ;
filename << "Voltage_" << N << ".dat" ;
std::eek:fstream 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.
 
R

Richard Heathfield

James Kanze said:
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? said:
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.
 
J

James Kanze

James Kanze said:
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.
 
R

Richard Heathfield

James Kanze said:
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).
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?
 
J

Jim Langston

vj said:
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.
 
R

Richard Heathfield

Jim Langston said:

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).
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top