Solved

Saving into a variable file name

Posted on 2014-09-25
3
179 Views
Last Modified: 2014-09-28
I need to write to a file name depending on a couple of variables and I'm having a hard time doing it. I got several different errors but couldn't find an answer anywhere in my books or the web on how it could be solved.

I'm using the following code and it works fine, but when I try to use the variables to use in the creation for the file and than it all goes wrong:

Working one
ofstream fout("C:/Users/rodrigo/Desktop/myfile.txt");
fout<<somevar<<yetanothervar;
fout.close();

NOT WORKING ONE:
int somevar;
int yetanothervar;
somevar = 1;
yetanothervar = 3;
string address;
address = "C:/Users/rodrigo/Desktop/"+somevar+yetanothervar+".txt";
ofstream fout(address);
fout<<somevar<<yetanothervar;
fout.close();

Any help would be much apreciated.
Cheers
0
Comment
Question by:Rodric MacOliver
[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
3 Comments
 
LVL 16

Expert Comment

by:choward16980
ID: 40344992
Add quotations on your address output?
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 40345029
You can't add a string and an integer to form a new string like you can in e.g. C# - in C++ you need a stringstream, e.g.

#include <sstream> // <-- add this one

int somevar;
int yetanothervar;
somevar = 1;
yetanothervar = 3;

stringstream ss;

ss << "C:/Users/rodrigo/Desktop/" << somevar << yetanothervar <<".txt"; // create file name

string address;
address = ss.str();

ofstream fout(address);
fout<<somevar<<yetanothervar;
fout.close();

Open in new window

0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 40345495
ofstream fout(address);

Open in new window

that will not compile. use address.c_str() as argument instead.

the usage of string streams could be improved by defining the following macro (you may put it into a common header file).

#define VARFILENAME(s) ((std::ostringstream&)((std::ostringstream())<<s)).str().c_str()

Open in new window

then you can do

std::ofstream fout(VARFILENAME("C:/Users/rodrigo/Desktop/" << somevar << yetanothervar <<".txt"));

Open in new window

the macro could be used not only for filenames but for any other purpose where required type is const char *.

Sara
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.

739 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