Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving into a variable file name

Posted on 2014-09-25
3
Medium Priority
?
182 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
3 Comments
 
LVL 16

Expert Comment

by:Chris H
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

Technology Partners: 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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

885 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