473,729 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ 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 4261

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(filenam e,"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...@g mail.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
SquareoftheHypo tenuse said:
Hi,

Try this....

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

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

sprintf(filenam e,"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, SquareoftheHypo tenuse
<squareofthehyp oten...@gmail.c omwrote:
Hi,

Try this....

FILE fp1;
char filename[30];

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

sprintf(filenam e,"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...@g mail.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 SquareoftheHypo tenuse
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<<data 1<<data2<<endl; "
Thankyou

May 24 '07 #6
"vj" wrote
SquareoftheHypo tenuse wrote:
>Try this....

FILE fp1;
char filename[30];

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

sprintf(filena me,"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<<data 1<<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_st r());

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

return 0;
}

--
Randy

May 24 '07 #7
On May 24, 7:05 am, vj <vijayjany...@g mail.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::ostringstr eam 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 objektorientier ter Datenverarbeitu ng
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...@g mail.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.i nvalidwrote:
James Kanze said:
On May 24, 7:05 am, vj <vijayjany...@g mail.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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #10

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

Similar topics

3
1556
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 index position within the function. The problem occurs in the following statement block: def create_apid_list(l_format,l_values): l_output = for t_curr_line in l_values: # ...for each list in the values list
3
23954
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
4786
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 longer write to the file. When this happens, fopen and fputs do not return an error. I've been researching large file support for Linux, and it all has to do with the regular 2-gig file size limit. If it's something obvious, sorry -- I'm a C...
8
5835
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 only get the file name, not the path. Can someone point out my error? See notes below code. .... Dim strFileNm As String Dim FileNm As New StringBuilder()
3
6104
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
1768
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
2560
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 user makes changes to a record using the application they have to option to click the APPEND button to overwrite there changes on the file. The file has a line of text for each record....... Here is the code for when they click the APPEND...
8
1950
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 follows 1. char filename; 2. int x=2; 3. filename="xxxxx"; But i do not know how to append 2 to filename. Thanks
5
2730
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 xls filename? I'm looking for an end result such as qryEmpItems_EMPLOYEENAME.xls Or better yet EMPLOYEENAME_Survey.xls Not sure if this is a possibility. Thanks!
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6016
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.