0

I am trying to use popen to use already installed gnuplot.exe in 64 bit windows

In my C program I am using the example as follows

    FILE *gnuplot = popen("C:\\Program Files (x86)\\gnuplot\\bin\\gnuplot.exe", "w");

However, the filepath is not recognized in Windows Command Line I am using Geany on windows which gives the following output.

[enter image description here]

How do I make the C program get the correct filepath ?

AAI
  • 290
  • 1
  • 3
  • 20
  • Without really knowing much about this topic, from what I can tell, `popen` is a posix command, so I would expect it would possibly only work with posix paths, and not Windows paths like you're giving it. – Random Davis Apr 10 '17 at 16:36
  • @RandomDavis SO says it is possible. POSIX is supported http://stackoverflow.com/questions/450865/what-is-the-equivalent-to-posix-popen-in-the-win32-api. Also the compiler does not throw unknown command error. It compiles and builds and executes successfully. – AAI Apr 10 '17 at 16:38
  • 1
    I didn't see anything in that question saying that the posix version of `popen` supports Windows paths. Also, of course it would have built fine, it's a runtime error after all. – Random Davis Apr 10 '17 at 16:40
  • 1
    I can't try but maybe `popen( "\"C:\\......exe\"", "w" );` could work – Ingo Leonhardt Apr 10 '17 at 16:42
  • @IngoLeonhardt I tried. it does not work. My concern is in the spaces in "Program Files (x86)" portion. I believe that is not being recognized in its entirety. Because the output (screenshot) says that the program goes upto 'C:\Program' . what happened to rest of the filepath ? – AAI Apr 10 '17 at 17:15
  • well it was worth trying ... obviously the string is split at whitespace, I guess the rest of the path is interpreted as command line parameters of the 'program' C:\Program. – Ingo Leonhardt Apr 12 '17 at 14:37
  • @IngoLeonhardt so how do I resolve the issue. Is there any work around ? Thanks. – AAI Apr 15 '17 at 17:53

1 Answers1

0

popen expects a shell command, so you want to pass

"C:\Program Files (x86)\gnuplot\bin\gnuplot.exe"

which is achieved using

popen("\"C:\\Program Files (x86)\\gnuplot\\bin\\gnuplot.exe\"", "w")
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Doesn't work ("\"C:\\Program Files (x86)\\gnuplot\\bin\\gnuplot.exe"", "w"); - Compilation error. However, popen("\'C:\\Program Files (x86)\\gnuplot\\bin\\gnuplot.exe'", "w"); compiles but says the directory name syntax is incorrect – AAI Apr 10 '17 at 17:18
  • 1
    You missed `\ `. /// Well, yeah, `'C:\\Program` isn't a valid path. – ikegami Apr 10 '17 at 17:28
  • Did you mean the last "\". Please explain what that "\" means because popen has to open the filename with .exe and not search for file beyond that character. Thank you. – AAI Apr 10 '17 at 19:44
  • I mean you didn't use the code I posted. Try using the code I posted. – ikegami Apr 10 '17 at 20:11
  • result: compile failed. Thanks – AAI Apr 10 '17 at 21:18
  • Sorry, I can't tell what mistake you made this time from that limited amount of information. Did you miscopy my code again? – ikegami Apr 10 '17 at 21:20
  • 1
    Sorry, I can't tell what mistake you made this time from that limited amount of information. The code I posted is syntactically correct. – ikegami Apr 10 '17 at 21:29