Solved

Saving into a variable file name

Posted on 2014-09-25
3
172 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:R Wolf
3 Comments
 
LVL 16

Expert Comment

by:choward16980
Comment Utility
Add quotations on your address output?
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now