473,473 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

append output filename

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
12 4238

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

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
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
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
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
"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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: waters | last post by:
I seem to have hit a snag. I am trying to add the list (l_local) as an item in the output list (l_output). "l_local" is a modified copy of the format-list "l_format", which will be updated by...
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
14
by: Aaron Couts | last post by:
I have a program that writes to a log file. It's compiled on RH Linux 7.3 (Kernel 2.4.18-18.7). It's using fopen in append mode. When the file reaches 51200000 bytes in size, the program will no...
8
by: WordVBAProgrammer | last post by:
I've been struggling with this for a few days now. It worked originally as plain VB-type strings, but for some reason ceased creating the FileNm. I changed the code to use StringBuilder, but...
3
by: juicy | last post by:
I have create 4 check box, every check on the check box will append text in textarea and any uncheck on check box will remove text in text area. Please give some guide on how to control this.
1
by: Ritesh Raj Sarraf | last post by:
Hi, I've got a problem here. def compress_the_file(zip_file_name, files_to_compress, sSourceDir): """ Condenses all the files into one single file for easy transfer """ try:
2
by: Gaby Sandoval | last post by:
I have this code. The user can read teh record from the text file just fine. They can click the NEXT button and it reads the next record (which is just the next line from the text file). If the...
8
by: aeo3 | last post by:
Hello Everybody; I would like to append a number to set of char, I do not like to use string, because In my project I am using several files. What I did but there is a problem with it is as...
5
by: artemetis | last post by:
I have a macro that is exporting records for a particular employee to an xls. The default output filename is qryEmpItems.xls Is there a way to have the macro append the employee name to the actual...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.