473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Incrementing the filename of a text file

I've been working on this problem for some time and I must say I'm
stump. Any help would be appreciated.

Basically what I'm trying to do is write the results of a loop to a new
text file with every pass. For example the user chooses that the loop
should run 10 times. I want to write the results of each pass of the
loop to a text file by incrementing the filename and creating a new
text file. So if it the loop went through 10 passes I would have
results1.txt, results2.txt, results3.txt, etc all the way up to
results10.txt

I don't know if I've been totally clear here but I'm grateful to anyone
who can help me figure something out.

Dec 7 '06 #1
4 1808
<ma*************@gmail.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
I've been working on this problem for some time and I must say I'm
stump. Any help would be appreciated.

Basically what I'm trying to do is write the results of a loop to a new
text file with every pass. For example the user chooses that the loop
should run 10 times. I want to write the results of each pass of the
loop to a text file by incrementing the filename and creating a new
text file. So if it the loop went through 10 passes I would have
results1.txt, results2.txt, results3.txt, etc all the way up to
results10.txt

I don't know if I've been totally clear here but I'm grateful to anyone
who can help me figure something out.
for ( int i = 0; i < some; ++i )
{
std::string filename("Myfile");
filename += favoritefunctiontoconvertintotcharorstdstring( i );
filename += ".txt";
}

One way to convert an interger to text is with a stringstream.

std::stringstream Convert;
Convert << i;
std::string IntAsString;
Convert >IntAsString;
Dec 7 '06 #2

Jim Langston wrote:
<ma*************@gmail.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
I've been working on this problem for some time and I must say I'm
stump. Any help would be appreciated.

Basically what I'm trying to do is write the results of a loop to a new
text file with every pass. For example the user chooses that the loop
should run 10 times. I want to write the results of each pass of the
loop to a text file by incrementing the filename and creating a new
text file. So if it the loop went through 10 passes I would have
results1.txt, results2.txt, results3.txt, etc all the way up to
results10.txt

I don't know if I've been totally clear here but I'm grateful to anyone
who can help me figure something out.

for ( int i = 0; i < some; ++i )
{
std::string filename("Myfile");
filename += favoritefunctiontoconvertintotcharorstdstring( i );
filename += ".txt";
}

One way to convert an interger to text is with a stringstream.

std::stringstream Convert;
Convert << i;
std::string IntAsString;
Convert >IntAsString;

can be done as below.
#include <iostream.h>
#include <stdlib.h>
#include<string.h>

void main()
{
char *st = new char[20] ;
char s[3] ;
for(int i = 0 ; i<10 ; i++)
{
strcpy(st,"result") ;
strcat(st,itoa(i,s,10)) ;
strcat(st,".txt") ;
cout<<" File name = "<<st<<endl;
}
}

Dec 7 '06 #3
Thanks alot that's got me fixed up.

Dec 7 '06 #4
vijay wrote
>

can be done as below.
#include <iostream.h>
#include <stdlib.h>
#include<string.h>

void main()
{
char *st = new char[20] ;
char s[3] ;
for(int i = 0 ; i<10 ; i++)
{
strcpy(st,"result") ;
strcat(st,itoa(i,s,10)) ;
strcat(st,".txt") ;
cout<<" File name = "<<st<<endl;
}
}
When you folloe up to Jim Langstons post, then I assume you think
this is a better way. I would disagree.

There is why:

1)
How many places do you have to change or check your code if I don't
want 10 but 100 filenames?
Jim: 1
vijay: 5! ( are they arrays big enough?)

if I want a diferent beginning of my filename..
Jim: 1
vijay: 3!

2)
You dont include the correct c++ headers. Some of your headers are
not needed by your code, nor should they have the ".h" endings!

3)
Using arrays is evil! To many things can go wrong with arrays, and
c++
offers a safer alternative namely std::string. That class was built
to avoid having to deals with strings. Its a standard class so
everbody reading the code knows what it does ( it handles stings),
and they know it is safe (it will handle any reasonable string given
to it)

4)
You give the wrong return type to main(). main() returns int. it
always has, and no compiler should accept this.

5)
You did not compile your code, did you... It would have caught a few
typos, and missing qualifications. cout should be std::cout, endl
should be std::endl

If you maded it this far, I will admit, that your solution would
probably very close to what I would do in C.

Bo Møller

--
Bo Møller

Hobby-Programmer
Dec 7 '06 #5

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

Similar topics

3
by: S.W. Rasmussen | last post by:
With the risk of being accused of multi-posting I would like to draw the attention to a serious visual basic/windows issue discussed in the microsoft.public.vb.bugs newsgroup. As pointed out below...
2
by: Csharper95 | last post by:
I want to get the file name and path from a SaveFileDialog (for a 'save as' operation) after the user has saved the information under a new file name (and presumably under a new path) ? I want...
5
by: Ariel Dolan | last post by:
Can I use HtmlInputFile to only get the selected file name but not actually upload the file? Clicking the control's Browse button let's the user select a file. All I need is the HtmlInputFile...
3
by: UJ | last post by:
How can I add a browse button to my asp.net page? What I need is a button they can press that will then let them select the file to upload to the server. And if anybody has any good code on how...
10
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly...
10
by: David Thielen | last post by:
Hi; I have help html pages for each page of my ASP.NET webapp. So for the page datasource.aspx, I have help\datasource.htm. Bu what I want when the hyperlink is clicked, for it to look for the...
3
by: =?Utf-8?B?ZGVlcGFr?= | last post by:
Hi I m facing a strange issue with filename I am attaching an attachment (may be a notepad file, video file or whatever) through a dummy customer page (customer.html) using a text box on...
18
by: RedLars | last post by:
Hi, How can check using .NET 1.1 that a string contains a valid filename for winxp? The application in question has a textbox where user can enter filename and only the filename. It should...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.