Connecting Tech Pros Worldwide Help | Site Map

Incrementing the filename of a text file

  #1  
Old December 7th, 2006, 06:45 AM
masterpaladin38@gmail.com
Guest
 
Posts: n/a
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.

  #2  
Old December 7th, 2006, 07:45 AM
Jim Langston
Guest
 
Posts: n/a

re: Incrementing the filename of a text file


<masterpaladin38@gmail.comwrote in message
news:1165475239.977281.141880@79g2000cws.googlegro ups.com...
Quote:
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;


  #3  
Old December 7th, 2006, 12:25 PM
vijay
Guest
 
Posts: n/a

re: Incrementing the filename of a text file



Jim Langston wrote:
Quote:
<masterpaladin38@gmail.comwrote in message
news:1165475239.977281.141880@79g2000cws.googlegro ups.com...
Quote:
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;
}


}

  #4  
Old December 7th, 2006, 01:35 PM
masterpaladin38@gmail.com
Guest
 
Posts: n/a

re: Incrementing the filename of a text file


Thanks alot that's got me fixed up.

  #5  
Old December 7th, 2006, 02:05 PM
Bo Møller
Guest
 
Posts: n/a

re: Incrementing the filename of a text file


vijay wrote
Quote:
>
>
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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM