iraj dehghan
iraj dehghan

Reputation: 21

expected ';' before numeric constant

I have the following code in C++, trying to write a xml file into a file, but it keeps giving me this problem "expected ';' before numeric constant

int main () {
  ofstream myfile;
  myfile.open ("example.xml", ios::out| ios::app| ios::binary);
  if (myfile.is_open())
  {
    myfile << "<?xml version="1.0" encoding="UTF-8"?> \n";
    myfile << "<?xml-stylesheet type="text/xsl" href="wufi1d.xslt"?> \n";
    myfile << "<WUFI1D> \n";

Can anyone help?

Upvotes: 2

Views: 869

Answers (7)

sinelaw
sinelaw

Reputation: 16553

You need to escape the quotations inside the string, like so:

myfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";
myfile << "<?xml-stylesheet type=\"text/xsl\" href=\"wufi1d.xslt\"?> \n";
myfile << "<WUFI1D> \n";

The compiler thinks the string is finished when it sees the first " - therefore you have to tell it that you mean to literally include it in the string, by escaping it \".

See here for the full list of characters' escape codes in C++.

Upvotes: 4

Qaz
Qaz

Reputation: 61910

You can't use raw quotation marks in a string because they signify the end of the string. Escape them instead:

myfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";
myfile << "<?xml-stylesheet type=\"text/xsl\" href="wufi1d.xslt"?> \n";
myfile << "<WUFI1D> \n";

You can also use a raw string literal in C++11, which treats everything (including whitespace) as part of a raw string:

myfile << 
R"(<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="wufi1d.xslt"?>
<WUFI1D>
)";

If you have brackets inside the string, put a delimiter around it: R"###(st(ri)ng)###"

The latter might be preferable for longer files, so that you don't have to escape everything, but keep in mind that all newlines, whitespace etc are included in the string, and that syntax highlighting can get messed up depending on whether the editor takes raw literals into account, as well as indentation if it's important that each line doesn't contain extra spaces at the beginning, since you'd then have to keep the literal to the very beginning of each line.

Upvotes: 1

imreal
imreal

Reputation: 10358

You have to escape the (") double quotes using backslash that are not string delimiters:

myfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";

Upvotes: 0

Mike Corcoran
Mike Corcoran

Reputation: 14565

embedded quotes need to be escaped.

this:

myfile << "<?xml version="1.0" encoding="UTF-8"?> \n";
myfile << "<?xml-stylesheet type="text/xsl" href="wufi1d.xslt"?> \n";

should look like this:

myfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";
myfile << "<?xml-stylesheet type=\"text/xsl\" href=\"wufi1d.xslt\"?> \n";

Upvotes: 1

andrewmu
andrewmu

Reputation: 14534

You aren't escaping the quotes in your output string e.g.

myfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";

Upvotes: 0

ApplePie
ApplePie

Reputation: 8942

You have double-quotes unescaped as shows the code-highlighter. Use \" when you want to show double-quotes in the outputted text.

Upvotes: 0

WhozCraig
WhozCraig

Reputation: 66194

Escape your dbl-quotes: I.e.

   myfile << "<?xml version="1.0" encoding="UTF-8"?> \n";

should be

   myfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";

As an example

Upvotes: 2

Related Questions