Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using visual studio 2012 and I have defined some macros for output directories,I want to use them in my C++ code instead of defining their absolute path like below :
C++
char *path="C:/Dir/Release/FILE.txt"; 

I am using ofstream to flush a text file:
C++
void Func_ID_Out(vector<string>& s){	
        ofstream myfile;
	myfile.open(path);
	int len=s.size();
	for (int i=0;i<len;++i)
	{
		myfile<<s.at(i)<<" ID : "<<func_hashcode(s.at(i))<<"\n";
	}

	myfile.close();
}


instead of doing this I want to use predefined VS MACRO for output directory as a argument for the funtion below
C++
myfile.open(PRE_DEFINED_MACRO/"FILE.txt")


Is it possible?Is there good mechanism to use Visual Studio Macros within our C++ Code body?
Posted
Updated 28-Oct-13 19:49pm
v3
Comments
Mohibur Rashid 29-Oct-13 5:51am    
along with enhzflep reply, in c/c++ you can easily join two constant string together.
i.e.
"c:\\" "filename.text";
#define path "c:\\"

openfile(path"filename.text");
Richard MacCutchan 29-Oct-13 6:06am    
Why do you need a macro like that, are you building different versions of the program, or do you want to change the path at run time?
nv3 29-Oct-13 6:35am    
You are talking about "Visual Studio Macros", which is something totally different from what you want. A Visual Studio Macro stores a sequence of UI operations for later repeated use.

In contrast, what you are talking about are C++ preprocessor definitions, also sometimes referred to as macros, but never in the context of "visual studio macros".

Back to your question: Of course it is possible to define a constant string like that and later join it with another string component. You will have to obey to the syntax for string literal concatenation. So remove that slash from the open statement and make the slash part of your define symbol.

This works for me. Basically, you'll want to use the string class to easily add the two strings together. You can make uniquePart a char* if you like, but unless you want to get messy with c-style strings, you should ensure path is a std::string.

C++
#include <iostream>
#include <fstream>
using namespace std;

#define definedVar "c:/"

//void someFunc(char *uniquePart)  // - this is fine too. Pick either.
void someFunc(string uniquePart)
{
    string path = definedVar;
    path += uniquePart;

    ofstream myFile;
    myFile.open(path.c_str());

    // do stuff with myFile here
    // 

    myFile.close();
}

int main()
{
    someFunc("/someFile.dat");
    return 0;
}
 
Share this answer
 
Comments
Buddhi Chaturanga 29-Oct-13 6:18am    
thank you.But your answer is alternative one.I have done that before.
enhzflep 29-Oct-13 6:47am    
You're welcome, but please, in the future you should detail what you've already tried. This will prevent the wasting of time by both the person that answers your question and yourself.
Off the top of my head the best way of using a VS macro in C++ code would be to associate your VS macro with a C++ preprocessor define using the /D command line parameter (properties->C++->preprocessor->preprocessor definitions).

Just make sure you have a decent default if someone doesn't set up the macro/externally define the command line parameter - there's nothing worse than a badly behaved build that accidentally writes stuff in weird places on your hard disk.
 
Share this answer
 
Comments
Buddhi Chaturanga 29-Oct-13 23:46pm    
thankz for the cooperation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900