Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 7th, 2006, 06:45 AM
masterpaladin38@gmail.com
Guest
 
Posts: n/a
Default 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.

  #2  
Old December 7th, 2006, 07:45 AM
Jim Langston
Guest
 
Posts: n/a
Default 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
Default 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
Default 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
Default 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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles