Solved

Placing a counter variable w/in double quotes

Posted on 1998-07-24
9
214 Views
Last Modified: 2010-04-10
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.
0
Comment
Question by:wtc108
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 

Author Comment

by:wtc108
ID: 1168567
Edited text of question
0
 
LVL 8

Expert Comment

by:trestan
ID: 1168568
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
 
LVL 6

Expert Comment

by:snoegler
ID: 1168569
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:alexo
ID: 1168570
>> 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
 
LVL 84

Expert Comment

by:ozo
ID: 1168571
(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
 

Author Comment

by:wtc108
ID: 1168572
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
 
LVL 6

Expert Comment

by:snoegler
ID: 1168573
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
 
LVL 1

Accepted Solution

by:
PC-Alex earned 50 total points
ID: 1168574
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
 
LVL 8

Expert Comment

by:trestan
ID: 1168575
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question