473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to convert a $(macros) into a string

Hi there,

I am writing a code to print the results for any given project into the corresponding solution directory. Here is my idea

//*** CODE***//

// In C/C++ properties, Preprocessor, "Preprocessor Definitions":
MY_SOLUTIONDIR=$(SolutionDir)

// In the cpp file:
#define OP OP2(MY_SOLUTIONDIR)
#define OP2(x) OP3(x)
#define OP3(a) #a

string print_folder = OP;
print(print_folder, my_results);


//*** RESULTS***//
MY_SOLUTIONDIR = C:\Projects\My_project
print_folder = "C: ProjectsMy_project" (here I lose the backslashes)

I need to convert MY_SOLUTIONDIR into a string for my print function. With my 3-level macro OP I manage to add quotation marks to get a string but I have a problem with the backslash as C++ requires double backslashes.

Any idea on how to convert the MY_SOLUTIONDIR into a 'good' string? Any other idea on how to use the $(SolutionDir) of the preprocessor for a function requiring a string as an input?

Many thanks,

Matthieu
Mar 28 '08 #1
6 5120
weaknessforcats
9,208 Expert Mod 8TB
C++ does not require double backslashes. Windows paths use a backslash and that is the C/C++ escape sequence. So to get one backslash you need \\ in the string.

Just go into your print_folder string and double up the backslashes. The compiler will force a backslash when the backslash escape sequence is entountered. Therefore, in your compiled string there will be one backslash.

BTW: There a five major features in C++ to replace macros. You should not be using macros in C++.
Mar 28 '08 #2
Sick0Fant
121 100+
BTW: There a five major features in C++ to replace macros. You should not be using macros in C++.
Tell that to my prof in college :-).
Mar 28 '08 #3
C++ does not require double backslashes. Windows paths use a backslash and that is the C/C++ escape sequence. So to get one backslash you need \\ in the string.

Just go into your print_folder string and double up the backslashes. The compiler will force a backslash when the backslash escape sequence is entountered. Therefore, in your compiled string there will be one backslash.

BTW: There a five major features in C++ to replace macros. You should not be using macros in C++.
Thanks for your answer,

But if you read again my post, you ll notice that there is no backslash to double since they are erased.
The problem is:
1) I cannot work on the macro since it is not a string and not a char[], it is just a raw text replaced by the preprocessor at compilation time. The only solution I found is to use another macro OP(x) #x to add quotation marks.
2) As soon as I convert the macro into a string, the backslashes are seen as an escape statement. I think it is IMPOSSIBLE to double the backslashes in the macro, first i need to convert it into a string. But if I try to do so, then I have a proble... see 1)

The only solution for me is to use another string class, like a verbatim class in C# which accepts single backslash...????
Who will be strong enought to break this problem...?
Mar 28 '08 #4
Sick0Fant
121 100+
Thanks for your answer,

But if you read again my post, you ll notice that there is no backslash to double since they are erased.
The problem is:
1) I cannot work on the macro since it is not a string and not a char[], it is just a raw text replaced by the preprocessor at compilation time. The only solution I found is to use another macro OP(x) #x to add quotation marks.
2) As soon as I convert the macro into a string, the backslashes are seen as an escape statement. I think it is IMPOSSIBLE to double the backslashes in the macro, first i need to convert it into a string. But if I try to do so, then I have a proble... see 1)

The only solution for me is to use another string class, like a verbatim class in C# which accepts single backslash...????
Who will be strong enought to break this problem...?
They already broke this problem by developing the C++ language. Use it to your advantage.
Mar 28 '08 #5
They already broke this problem by developing the C++ language. Use it to your advantage.
Anybody who understands the question?
I have a macro FOLDER with raw text C:\Myproject
I need to get a string "C:\\Myproject"

2 alternatives:

1)
OP(x) #x
string my_folder = OP(FOLDER )
doublebackslash(my_folder)

Problem is the single backslash is erased in the second line when I convert it to a string. So no way to double it since it is seen as an escape statement and then erased.

2)
double the backslash in the macro first.
Problem is it is impossible since it is a macro, just raw text replaced by processor at compilation time, it s not a string or a char[], it s nothing so icant do anything before to convert it

need to convert it first, but if I convert it then the single backslash is a problem...
Any idea? any other way to get a 'clean' string with the solution directory, sinble or double backslashes but a STRING!!!
Mar 28 '08 #6
Sick0Fant
121 100+
If you are using C++, don't use macros!! Crikey.
Mar 28 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Chen Shu | last post by:
Hi there: These days I came to a problem that I failed to convert a binary number to a hex number using a macro. For example: int a = BINARY4(0101) will become int a = 0x5; int a =...
9
by: ka1cqd | last post by:
I have been looking all over the place for a method to take command line arguments and convert them to a string or wstring so i can process the data and then covert the resulting strings to...
4
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
9
by: keliie | last post by:
Hello (from Access novice), I'm building a switchboard form (using a Treeview object). The treeview is populated by two tables (tblSwitchboardParent and tblSwitchboardChild). Within...
8
by: Frank | last post by:
I have a c program that uses the "A" version of API files. Since it runs on XP I'd guess it would be better if it used the "W" versions. Why is it using the "A" version. I looked in...
12
by: horacius.rex | last post by:
Hi, I have a code that in some part of the program calculates 1/x for a lot of different x's. About 1 of 100 times x is equal to zero, so when I print the result I obtain inf. I wonder if there...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
8
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello Everything in .Net is an object and CTYPE converts one datatype to another datatype so why use Cstr, Cint or any other method to convert from one datatype to another?
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.