Incrementing the filename of a text file

  • Thread starter masterpaladin38
  • Start date
M

masterpaladin38

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

Jim Langston

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

vijay

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


}
 
?

=?ISO-8859-15?Q?Bo_M=F8ller?=

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
 

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

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top