Placing a counter variable w/in double quotes

I'm a beginner at C++ with a project to complete where I have a variable amount of data output files (~100)  I need to write to in my program.  I originally had 100 files that I painstakinly typed out in a two-dimensional array format that I was able to systematically open each loop of 100 times for writing to.  But now I need to fix the program so that I can have a variable amount of files, so I was wondering how I could create a loop to declare a varying amount of output files with a given name (such as File1.dat, File2.dat, . . . Filex.dat).  My only thought was to somehow put a counter within the quotations of the array format ("File(counter).dat") but I have no knowledge on how to do this.  Thank you for any help you can provide.
wtc108Asked:
Who is Participating?
 
PC-AlexConnect With a Mentor Commented:
wtc108,

try this code:

#typedef DAT_FILE_STRING char[20]

const int nNumberOfFiles = 100;
int i;
DAT_FILE_STRING Fileout[nNumberOfFiles];

for (i = 0; i < nNumberOfFiles; i++)
{
    sprintf(Fileout[i], "File%d.dat", i);
}

this does exactly what you want.
0
 
wtc108Author Commented:
Edited text of question
0
 
trestanCommented:
If you want to append a number to the end of a string, you can use _itoa() to convert the number to char, then add it to the end of the string. I think in this way, you do not need to type each file name by yourself.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
snoeglerCommented:
One other way would be the following:

int counter;
char temp[256];
for(counter=0;counter<100;counte{r++) {
sprintf(temp,"file%d.dat",counter); // this produces something like "file<counter>.dat"
puts(temp);
}

This example will output the following:
file0.dat
file1.dat
file2.dat
.
file99.dat

0
 
alexoCommented:
>> sprintf(temp,"file%d.dat",counter);
Bad karma!  In C++ you use a strstream (or stringstream, depending on you compiler's lavel of conformance).
0
 
ozoCommented:
(I might have thought bad karma would have been having to use different things depending on your compiler's level of conformance, rather than something more portable)
0
 
wtc108Author Commented:
I got the file list to output using the answer provided by Snoegler, but this didn't allow me to use these strings for opening files.  What I need is to open 1 file at a time (each consecuative iteration) starting at File1.dat through File<x>.dat.  I need to use an

outFile.open("File1.dat")  up  to "File<x>.dat"

  The only way I thought I'd be able to accomplish this would be to have a two-dimensional array

char Fileout[100][20] = {"File1.dat", "File2.dat", . . . "File<x>.dat"};

 so that I could use a file open statement like

 outFile.open(Fileout[counter]

 at the beginning of my loop of each iteration.  But this caused the excessive typeing in the Fileout declaration and lack of varability in number of files.  I was wondering how I could go about storing the strings, that is

 "File1.dat" into element Fileout[0], . . . "File<x>.dat" into element Fileout[x-1]

, by using some type of loop.  In the answer you provided, I tried editing out the puts(temp) line and adding a line to copy the temp string into the first element of a two-dimensional array but I could not get this to work due to my beginning skills at programming.  
If you can think of a way to put a counter inside the quotes and then copy these individual strings to each element of  a two dimensional array for use of opening files please let me know or if you can think of another way around my little problem (I'm sure there's plenty).  Thanks a lot.
0
 
snoeglerCommented:
I think i didn't precisely answer your question. Consider the following:

char filename[200];
FILE outfile;

for(i=0;i<100;i++) {
sprintf(filename,"file%d.dat",i);   // filename contains for example with i==12 "file12.dat"
outfile.Open(filename);                // outfile opens file "file<i>.dat"
DoSomethingWith(outfile);         // here you could do what you want with "file<i>.dat"
outfile.Close();                             // close the file
}

To show you a working example:

#include <stdio.h>

int main(int argc,char *argv[])
{
   FILE *fp;
   int counter;
   char filename[200];

   for(counter=0;counter<100;counter++) {
      sprintf(filename,"file%d.dat",counter);
      fp  =fopen(filename,"wb");
      fprintf(fp,"This is the file %s\n",filename);
      fclose(fp);
   }
}
0
 
trestanCommented:
It is a bad habit to reject an answer because the codes were not written out for you. You can get a readily avaible codes from here for once, but you can not expect to let others do your job all the time. You must learn from the clues provided and try to coding by yourself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.