Frage

I want to output a line into a .plt file that says "one-D Hydro" with the double quotation marks and so far I have this problem.

#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
        fstream gnuplot_file;        

        gnuplot_file.open ("sod.plt");
        gnuplot_file<<"set title"<< ""one-D Hydro""<<std::endl;
        gnuplot_file.close();
        system("gnuplot.exe sod.plt");


        return 0;
}

Line 11 will not allow it to compile because I can't seem to close the statement. The error is just as useless by the way.

gnuplot_call.cpp|11|error: expected ';' before 'one'|

War es hilfreich?

Lösung

With C++03 (or even C) use backslashes to escapes double-quotes in string literals:

    gnuplot_file << "set title" << "\"one-D Hydro\"" << std::endl;

Notice that gnuplot may require you to also escape some characters, e.g. if you wanted the title to contain quotes!

With C++11 you could use raw string literals, e.g.

   gnuplot_file<< R"*(set title "one-D Hydro")*" << std::endl;

BTW, you could be interested by popen(3) and pclose, if your operating system and C++ library provides them. You would just popen the gnuplot process and send commands to it, finally pclose-ing it.

Andere Tipps

Try to include escape character [i.e.,back slash] in the code where you are trying to add double quotes. For example:

"\"one-D Hydro\""

btw why are you using std:: once you have defined namespace for it you can directly use endl.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top