Dynamic Variable Names

Posted on 2003-03-13
Medium Priority
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.
Question by:aomega
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

Expert Comment

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


Accepted Solution

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++)
          // write stuff to your file
          outFile << "File" << i;

     return 0;

Author Comment

ID: 8131208
Good stuff =)

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