输出字符文字 { 以流C++

Outputting character literal { to fstream C++

本文关键字:以流 C++ 文字 字符 输出      更新时间:2023-10-16

我正在尝试写入C++文件的一部分,以将文本输出到要由gnuPlot读取的文件中。我想在我的输出文件中包含字符 { 和 },以便 gnuplot 可以以类似于 laTex 使用的样式读取它们。

我的代码的相关部分如下:

std::ofstream gnuplotFile2;
gnuplotFile2.open("FourierImages.gpi");
gnuplotFile2 << "set xlabel "Wavenumber (m^" << '\u007B' << "-1" << '} << ""n";

在这里,我同时使用了 \u007B 和 } 来尝试输出 { 或 }。我也尝试只包括"...(m^{-1})";和"...(m^\{-1\}\m";.

我的输出文件始终读取

set xlabel "Wavenumber (m^-1)"

它生成一个图形/图表,标签在上标中具有 - 字符,但 1 作为普通脚本。

如何将 { 和 } 字符输出到文件?我想要

set xlabel "Wavenumber (m^{-1})"

除非我遗漏了什么,否则只需使用字符 {} ,就像这样

$ cat test.cc
#include <iostream>
#include <fstream>
int main() {
  std::ofstream gnuplotFile2;
  gnuplotFile2.open("FourierImages.gpi");
  gnuplotFile2 << "set xlabel "Wavenumber (m^{" << "-1" << "})"n";
}
$ g++ test.cc && ./a.out && cat FourierImages.gpi
set xlabel "Wavenumber (m^{-1})"