Solved

Placing a counter variable w/in double quotes

Posted on 1998-07-24
9
210 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now