简体   繁体   中英

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?

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++.

Escape your dbl-quotes: Ie

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

should be

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

As an example

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";

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.

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

您没有转义输出字符串中的引号,例如

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

你必须使用不是字符串分隔符的反斜杠来转义(“)双引号:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM