?
Solved

Dynamic Variable Names

Posted on 2003-03-13
3
Medium Priority
?
223 Views
Last Modified: 2010-05-18
I am trying to create a file stream and output destination which is distinct within each iteration of a for loop.  Would using dynamic variable names be a solution for this?  I have never used dynamic variables before so how would they be implemented? Or would there be another way around this?  Thanks.
0
Comment
Question by:aomega
[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
3 Comments
 
LVL 1

Expert Comment

by:jadams117
ID: 8126783
If I understand your question correctly you don't need to use dynamic variable names. You simply need to close the file and then open the new one for each iteration as follows..

#include <fstream>

int main
{

}
0
 
LVL 1

Accepted Solution

by:
jadams117 earned 200 total points
ID: 8126847
Whoops - I think I pressed the button too early..

#include <fstream>

int main()
{
     const int numIterations = 2;

     std::string filename[numIterations];
     // fill filenames array with whatever you want
     filename[0] = "File0.txt";
     filename[1] = "File1.txt";

     std::ofstream outFile;

     for (int i = 0; i < numIterations; i++)
     {
          outFile.open(filename[i].c_str());
          // write stuff to your file
          outFile << "File" << i;
          outFile.close();
     }

     return 0;
}
0
 

Author Comment

by:aomega
ID: 8131208
Good stuff =)
0

Featured Post

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.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 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