I am trying to use Gnuplot on Windows with gnuplot_i.hpp. When I type "gnuplot" into cmd everthing works, so the PATH variable should be set correctly. This is my code:
#include <iostream>
#include "gnuplot_i.hpp"
using std::cout;
using std::endl;
int main(int argc, char* argv[]) {
try {
Gnuplot g1("lines");
} catch (GnuplotException ge) {
cout << ge.what() << endl;
}
return 0;
}
The output is Can't find gnuplot neither in PATH nor in "C:/program files/gnuplot/bin" .
When I add the line
Gnuplot::set_GNUPlotPath("C:/gnuplot/bin/");
it just changes to Can't find gnuplot neither in PATH nor in "".
What am I doing wrong here?
Found the answer myself: For some reason gnuplot_i.hpp expects your exe to be called pgnuplot.exe instead of gnuplot.exe ... Now everything works.
Related
I have some trouble with producing files in C++. I consulted this answer here but when I try using it, it doesn't produce a file. What I wrote:
//~/Documents/Test_CPP/ex2/main_2.cpp
#include <fstream>
int main()
{
std::ofstream file("Hello.txt");
// Hello.txt has been created here
}
I compile it with the command g++ main_2.cpp and run it with ./a.out. I don't really know what could go wrong here, except theorizing that the file might be produced not in the current directory but somewhere else. So I tried changing Hello.txt to ~/Documents/Test_CPP/ex2/Hello.txt, which doesn't change anything. What exactly am I doing wrong here?
I have encountered this problem on macOS with Xcode if you use some IDEs you should point to build-dir.
My suggestion: use std::filesystem::current_path(). It will give full path to you elf\exe dir.
#include <string>
#include <iostream>
#include <filesystem>
#include <fstream>
int main() {
std::string file_name{"Hello.txt"};
auto path{std::filesystem::current_path()};
path = path / file_name;
if (std::filesystem::exists(path)) {
std::filesystem::remove(path);
}
std::ofstream out_stream(path, std::ios::out);
if (!out_stream.is_open()) {
std::cerr << "Error open file" << std::endl;
return -1;
}
out_stream << "test" << std::endl;
out_stream.close();
return 0;
}
This can sometimes happen if you do not properly terminate the connection to the file
EG.
file.close();
This must be done before the program terminates.
I've been unable to parse the executable with the build of the following code:
#include <iostream>
#include <fstream>
#include <string.h>
#include <windows.h>
using namespace std;
int main(int argc,char** argv)
{
if(argc==3)
{
if(strcmp(argv[1],"-i")==0)
{
string DueEXE, DueVER;
DueEXE = DueVER = "";
DueEXE += argv[2];
cout << endl << DueEXE;
DueVER += "\"" + DueEXE + "\" -ver > \"C:\\\\DueVER\"";
cout << endl << DueVER << endl << endl;
system(DueVER.c_str());
}
}
return 0;
}
Prerequisites: 1. DueEXE holds the file location to an *.exe that prints (in console) its version when used with -ver or --ver.
2. I use Windows XP SP3.
3. I use Code::Blocks with MinGW32, if that matters.
Problem encountered:When I execute my build using cmd as follows-1. build.exe -i "D:\OA 2\Due.exe" 2. build.exe -i "D:/OA 2/Due.exe", the result is something like-
D:/OA 2/Due.exe
D:/OA 2/Due.exe -ver > "C:\DueVER"
'D:/OA' is not recognized as an internal or external command,
operable program or batch file.
I know that I have to parse the 3rd argument with quotes.
Additionally, the result should be like-
"D:/OA 2/Due.exe"
"D:/OA 2/Due.exe" -ver > "C:\DueVER"
The entire Due.exe file location must be covered with quotes, but that doesn't happen.What's going wrong in the code?
Oh!
I forgot to call the system() function with the variable covered in quotes.
Replacing system(DueVER.c_str()) with system(("\""+DueVER+"\"").c_str()) did my work.
I made an Xcode project. Like this:
Mac OS X -> Application -> Command Line Tool
I chose C++
And then I saved it in a folder.
Now I opened it in Xcode, there is a file named main.cpp under the project name title, so I opened that.
So main.cpp contains (location: Desktop/CPP/learn/learn/main.cpp)
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
Now I click on File > New File and I selected C++ file without header, I named it main1.cpp
So that main1.cpp now just has
#include <stdio.h>
I replaced that and made main1.cpp to
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "MAIN1DOTCPP!\n";
return 0;
}
Then I ran main1.cpp, And the out put was "Hello World", I have been trying to get the output of the second file, That is "MAIN1DOTCPP" for like 2 hours now, Can someone please help?
Alright, so I could have sworn this worked in my program earlier, but now I'm being driven mad by std::fstream. I just want to open a file from command line arguments, ie.
./main Program1.S
should open the file Program1.S and scan it.
Here is how I set up a open_file() function in my code:
#include <string>
#include <iostream>
#include <fstream>
void open_file(std::fstream &ifp, std::string file_name) {
ifp.open(file_name, std::ios::in | std::ios::out);
if(ifp.fail()) {
std::cout << "File not found." << std::endl;
exit(1);
}
}
void close_file(std::fstream &ofp) {
if(ofp.is_open()) {
ofp.close();
return;
}
std::cout << "This file is not currently open" << std::endl;
}
int main(int argc, char * argv[]) {
std::string in_name;
in_name = argv[1];
std::fstream ifp;
open_file(ifp, in_name);
// do some processing
close_file(ifp);
return 0;
}
Now, I compile my program using (unfortunately I am required to use c++03): g++ -g -std=c++03 -Wall -pedantic main.cpp -o main
Compilation works and provides no errors, but when running the program using: ./main Program1.S, it goes to File not found in open_file(). I even checked what was in argv[1] and it is definitely a file that is in the current working directory. Is there something wrong with the way I am doing this?
Check to make sure your file has been added to your project folder. Otherwise, you need to specify a file path within your computer ex. "/Mac HD/Documents/myfile". The program has no idea what to do with a external file name without its file path. Hope this helps.
I am trying to create a directory inside program files, but my code failed. I can make folder directly on the root, so this is very strange I think.
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <direct.h> // _mkdir
int _tmain(int argc, _TCHAR* argv[])
{
// Make directory
if(_mkdir("C:\\Program Files\\MyProgram") == 0 ){
cout << "Folder created" << endl;
}
else{
cout << "Folder creation failed" << endl;
}
// Pause program
system("PAUSE");
return 0;
}
Edit:
Thank you. I understand that c:\Program files\MyProgram is only for install files, but where should I put save files? Example save files for a C++ game. Should they be stored in "C:\Users\Username\Documents\MyProgram"?
The proper folder name to write depends on version and localization.
A simple way to get that name is to search for the APPDATA environment variable.
See here to get an environment variable, and here about the recognized environment variables and meanings.