gnuplot in c++ - path error [duplicate] - c++

This question already has an answer here:
gnuplot-cpp cannot feed command to pipe
(1 answer)
Closed 8 years ago.
I'm trying to implement the code below from this site:
https://sites.google.com/site/bettereaclone/introduction/gnuplot/c-example-gnuplot-1
Code:
#include <iostream>
#include "gnuplot_i.hpp"
#include <windows.h>
#include <conio.h>
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
try
{
Gnuplot g1("lines");
cout << "*** plotting slopes" << endl;
g1.set_title("Slopes\\nNew Line");
cout << "y = x" << endl;
g1.plot_slope(1.0,0.0,"y=x");
cout << "y = 2*x" << endl;
g1.plot_slope(2.0,0.0,"y=2x");
cout << "y = -x" << endl;
g1.plot_slope(-1.0,0.0,"y=-x");
g1.unset_title();
}
catch (GnuplotException ge)
{
cout << ge.what() << endl;
}
return 0;
}
I installed the gnuplot (http://www.gnuplot.info/download.html)
gnuplot_i.hpp file (https://code.google.com/p/gnuplot-cpp/source/browse/trunk/gnuplot_i.hpp)
When I run this code, I get this problem:
The error:
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.
I've no idea why. thanks!!

Looks like path to gnuplot is malformed, not in quotation marks or simply not read properly.
Try forcing the path by uncommenting this line and correcting it to point gnuplot directory on your machine:
// Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");

Related

How to ask, programmatically, a compiler to compile a file in C++?

The following is my C++ program:
main.cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
return 0;
}
When the above program is executed, it creates a text-file named "firstFile.cpp" containing the following code:
firstFile.cpp
#include <iostream>
int main() {
std::cout << "hello world" << std::endl;
return 0;
}
which, when executed, prints "hello world" on the screen.
So, I would like to add to the main.cpp file lines of code asking GCC to compile the new firstFile.cpp just created.
I am using GNU gcc on both platform Ubuntu and Windows.
Is it possible to get any error code form the call to the compiler? If not why.
This is not too difficult using the std::system command. Also raw string literals allow us to insert multiline text which is useful for typing in program parts:
#include <cstdlib>
#include <fstream>
// Use raw string literal for easy coding
auto prog = R"~(
#include <iostream>
int main()
{
std::cout << "Hello World!" << '\n';
}
)~"; // raw string literal stops here
int main()
{
// save program to disk
std::ofstream("prog.cpp") << prog;
std::system("g++ -o prog prog.cpp"); // compile
std::system("./prog"); // run
}
Output:
Hello World!
gcc is an executable, so you have to use either system("gcc myfile.cpp") or popen("gcc myfile.cpp"), which gives you a filestream as result.
But since you are generating code anyways, you don't even need to write it to a file. You can open the gcc proces with FILE* f = popen("gcc -x ++ <whatever flags>"). Then you have you can write your code with fwrite(f, "<c++ code>"). I know this is c and not really c++ but it might be useful. ( I don't think there is a c++ version of popen()).
All you need to do is add the following line after you create your file.
system("g++ firstFile.cpp -o hello");
Works on OS X so I hope it will work for you too.
To use the command line of a compiler in source file use system function.
Syntax of which is :
int system (const char* command); //built in function of g++ compiler.
In your case, it should be like
system("g++ firstFile.cpp");
PS: system function does not throw Exceptions.
Program
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
system("g++ firstFile.cpp");
return 0;
}
Depending on what you actually want to achieve you could also consider embedding some C++ compiler into your application.
Note that this is by far not as easy as calling an external executable, and might be subject to licence restrictions (GPL).
Also note that by using std::system or a similar mechanism you add the requirement on your target environment to actually have the called compiler available (unless you somehow bundle it with your application).
Something like this:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
system("c firstFile.cpp");
return 0;
}
or whatever command is appropriate for the compiler you're using.

Odd fstream issue

I've got an odd error with all my programs I've written no longer reading the files they're referenced to even if they're in the same directory. I tried old programs that I've written and tested and haven't touched for months yet now they return my ERROR message. I even wrote a simple program to test it:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream openFile;
int num, cnt=0;
openFile.open("data.dat");
if (!openFile) {
cout << "ERROR opening file" << endl;
} else {
cout << "I'm open!" << endl;
while(openFile >> num)
cout << num << endl;
cnt++;
}
cout << cnt << endl;
system("PAUSE");
return(0);
}
Information on file:
1
2
5
66
9
4
2
This returns the ERROR message and cnt at 0. The file is located in the exact same directory as the executable.
What exactly could be causing this? Is there an issue with my program?

How to create UI in C++ visual [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to C++ and visual studio, can anyone please help me in creating an UI for below requirements?
Requirement: There is a .txt file which consists of 10 test cases. In the UI, I(tester) should be able to select his/her choice of test cases to run.
Try using a MFC project. You can find drop-down menus in GUI toolbox.
Learn further in MFC.
refer this - https://www.youtube.com/watch?v=6hSYZdvQ3s4&index=1&list=WL
Consider the possibility to use command line interface as the following template shows:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
// check that parameter is given in command line when program is run
if(argc != 2) // if number of parameters is insufficient
{ // explaine and stop
cout << "Please, run a program with parameter: name of file with testcases" << endl;
cout << "For example:" << endl << " " << argv[0] << " tests.txt" << endl;
return 1;
}
// try to open file with test cases
ifstream testfile;
testfile.open(argv[1], ifstream::in);
if( !testfile.is_open() )
{
cout << "ERROR: File " << argv[1] << " cannot be open!" << endl;
return 2;
}
// read test cases from file
string testCaseName;
char answer;
while( getline(testfile, testCaseName).good() )
{
if(testfile.eof())
{
break;
}
if(testCaseName.length() == 0)
{
continue;
}
cout << "Would you like to execute testcase " << testCaseName << " ? (Y/N): ";
cin >> answer;
if( answer == 'Y' || answer == 'y')
{
cout << "Test execution... " << endl;
// run test case
// . . .
// report test results
// . . .
}
}
return 0;
}
To run your test with such program you should type in the command line:
programname test_file.txt

Using a C++ .exe with C++ : C++ception [duplicate]

This question already has answers here:
launch an exe/process with stdin stdout and stderr?
(3 answers)
Closed 8 years ago.
I need some help on this subject.
I have a C++ .exe that I want to open with C++ and then write some arguments in the console.
Here is an example of what I want to do:
Lets assume an executable whatsyourage.exe with this code (in reallity, I don't have the corresponding code):
#include <iostream>
using namespace std;
int main()
{
int age = 0;
cout << "What's your age ?" << endl;
cin >> age;
cout << "You are " << age << " year !" << endl;
return 0;
}
and I want to do something like:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";// in the same library
std::string const command = chemin+" "+ invit;
system(command.c_str());
}
I want to write the age (21).
Can someone please help me?
Here is the answer:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";
FILE* pipe = _popen(chemin.c_str(), "w");
if (pipe == NULL) {
perror("popen");
exit(1);
}
fputs("30", pipe);// write the age into the pipeline
pclose(pipe); // close the pipe
}
The popen() function from POSIX does what you are looking for. It allows you to execute a program (like system) while getting a file handle on its input/output streams.
For Windows, if popen() is not available, you can use the CreatePipe() & co functions to do the same thing; check out this question for some pointers.
The second snippet you added is good, and the problem is with the first code. In order to handle the command line from a program, you have to define the main as int main(int numberOfArguments, char* arguments[]) (often people use the shorter version - main(int argc, char* argv[]), but you can name the arguments as you wish). Then, if you pass an argument to the function, it will be in argv[1], since argv[0] is always the path to the executable. So the first program should look like this:
int main(int numberOfArguments, char* arguments[])
{
if(numberOfArguments>=2)
cout << "You are " << arguments[1] << " year !" << endl;
else
cout << "Too few arguments passed!" << endl;
return 0;
}

Why doesn't my function produce output

I'm doing a C++ assingment for a class and I haven't used C++ in a decade so this might be something I'm missing that is simple; however ,I can't seem to figure it out.
I have a class I defined with a function that is producing no output; it looks like it's not even running and I don't have a clue why. Could someone point out my problem to me?
Issue: cout from the function getwords of the class readwords doesn't display any results.
Here is my class:
class readwords {
private:
char c;
//string aword;
public:
void getwords(std::istream& file) {
cout << "I got here" << std::flush;
/*while(file.good()) {
cout << "I got here\n";
c = file.get();
if(isspace(c)) cout << "\n"; //continue;
if(isalnum(c)) {
cout << c; //aword.insert(aword.end(),c);
}
}
*/
}
};
Which is being called from my main:
#include <fstream>
#include <stdlib.h>
#include "lab1.h"
using namespace std;
readwords wordsinfile;
words wordslist;
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) {
// Looks like we have no arguments and need do something about it
// Lets tell the user
cout << "Usage: " << argv[0] <<" <filename>\n";
} else {
// Yeah we have arguements so lets make sure the file exists and it is readable
ifstream ourfile(argv[1]);
if (!ourfile.is_open()) {
// Then we have a problem opening the file
// Lets tell the user and exit
cout << "Error: " << argv[0] << " could not open the file. Exiting\n";
exit (1);
}
// Do we have a ASCII file?
if (isasciifile(ourfile)) {
cout << "Error: " << argv[0] << " only can handle ASCII or non empty files. Exiting\n";
exit(1);
}
// Let ensure we are at the start of the file
ourfile.seekg (0, ios::beg);
// Now lets close it up
ourfile.close();
}
// Ok looks like we have past our tests
// Time to go to work on the file
ifstream ourfile2(argv[1]);
wordsinfile.getwords(ourfile2);
}
Thank you for any help you can provide.
Try to use a debugger. Most IDEs (NetBeans, Code::Blocks, etc) provide an interactive interface with gdb.
I just compiled and ran your code, but nothing wrong with the code itself,
except that I needed to include to use the 'cout' method.
"I got here" has been successfully displayed in my ubuntu machine.
What is your execution environment? You should check it first.
The problem appears to be redefining my own class. When actually coding the function I needed to use:
in readwords::countwords(std::istream& file) {
....
}
Once doing this output produced fine.