Solved

Placing a counter variable w/in double quotes

Posted on 1998-07-24
9
213 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

839 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