Solved

Saving into a variable file name

Posted on 2014-09-25
3
177 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: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 33

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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