Link to home
Start Free TrialLog in
Avatar of wtc108
wtc108

asked on

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.
Avatar of wtc108
wtc108

ASKER

Edited text of question
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.
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

>> sprintf(temp,"file%d.dat",counter);
Bad karma!  In C++ you use a strstream (or stringstream, depending on you compiler's lavel of conformance).
Avatar of ozo
(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)
Avatar of wtc108

ASKER

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.
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);
   }
}
ASKER CERTIFIED SOLUTION
Avatar of PC-Alex
PC-Alex

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.