469,927 Members | 1,763 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

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 1604
<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ariel Dolan | last post: by
3 posts views Thread by UJ | last post: by
10 posts views Thread by Brian Gruber | last post: by
10 posts views Thread by David Thielen | last post: by
3 posts views Thread by =?Utf-8?B?ZGVlcGFr?= | last post: by
18 posts views Thread by RedLars | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.