?
Solved

Saving into a variable file name

Posted on 2014-09-25
3
Medium Priority
?
181 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 1000 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 35

Assisted Solution

by:sarabande
sarabande earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

718 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