$ sign (as in BASH) to represent variable

Hi,

I am converting a bash script to a C++ code and to develop it further with C++
I am also not so experienced in C++.

My question is I have a HTML file with variables represented with "$" sign for BASH variable.

Part of the HTML code is here that works with BASH script.

The variable "status" is represented with "$status"

<span style="font-size:12px;"><strong><span style="font-family:times new roman,times,serif;">Status : &nbsp;$status</span></strong></span></div>

Now I have many lines like this. If I use C++ "ofstream" to define a file & I have to use operator "<<" to add these lines to html file after replacing variables with "<<status<<" and also escape all double quotes with \.

Is there a better way to replace variables in a HTML file?

I find it is easy with BASH as it uses "$" but in C++, we have to add "<<" at the beginning & end of each variable & also escape all double quotes.

I searched Google but it did not help much.

Any idea, tip, links to a solution are welcome.

Mathew
Maybe a c++ template engine is what you need

I am not an expert

there is a discussion here with some links to template libraries
http://stackoverflow.com/questions/355650/c-html-template-framework-templatizing-library-html-generator-library

These vary a lot in complexity, probably a simple one is enough


Simple solution: use C's "fprintf" function. Other solutions: make your own function, or find someone who has already and learn how to use theirs.
It sounds like you're just asking for a global search and replace. Try this in bash:

$ echo asdf \$asdf asdf > junk
$ cat junk
asdf $asdf asdf
$ cat junk | perl -pe 's/\$(\w+)/" << \1 << "/g' > fixed
$ cat fixed
asdf " << asdf << " asdf
$ 


Now if you were to start each line with cout << " and end it with " << endl; , it should do what I think you wanted.

Oh, I forgot about escaping double quotes... Try this instead:
$ cat junk | perl -pe 's/"/\\"/g' | perl -pe 's/\$(\w+)/" << \1 << "/g' > fixed
Last edited on
Oh ha.. finally I saw some Perl RE input in this forum! Those cryptic looking characters are stirring up my brain-cells again :P

PS they won't be cryptic looking if you spend a lot of time formulating RE input
Topic archived. No new replies allowed.