system call not opening .jar - c++

I am trying to open a .jar file, which is located at C:\Users\MyUser\ThisProgram.jar. I want to open it directly, using a system call, instead of having to use terminal manually. My current code is as follows:
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string str2 = "java -jar C:\\Users\\MyUser\\MyProgram.jar";
const char* cmd2 = str2.c_str();
system(cmd2);
}
I am getting an error that says "Unable to access jar file ThisProgram.jar". What can I do to fix this?

Related

JsonCpp can't find file when I use get_current_dir_name()

I am trying to parse a json file within my program:
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/json.h>
#include <unistd.h>
#include <stdio.h>
int main(){
std::string plan { get_current_dir_name() };
plan += "directory/file.json";
read_json(plan); // A function that reads a json file using jsoncpp
}
Output:
Error: Json File not found!
However when I manually write the entire path:
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/json.h>
#include <unistd.h>
#include <stdio.h>
int main(){
std::string plan { entire_file_path };
read_json(plan); // A function that reads a json file using jsoncpp
}
Output:
File found and read!
I thought maybe there is a spelling mistake but when I use std::cout on both of the paths, there is not a single difference. I'm not sure what is causing this issue.
Using std::filesystem built-in to C++17:
namespace fs = std::filesystem;
fs::path path = fs::current_path() / "directory" / "file.json";
read_json(path.string());

No output after I open file

I am setting up my environment to run a C++ program on VSCode in Windows 10. I have MINGW installed and the path (C:\MinGW\bin) added to my environment variables. I have added the appropriate extensions in VSCode.
In my hello world program. It works just fine. However, when I open a file, nothing prints from anywhere in the entire program.
I am compiling like this:
g++ main.cpp -o main
And running the program like this:
./main test.txt
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
cout <<"Here\n";
ifstream file; //This is the line that causes no output
file.open(argv[1]);
return 0;
}
The file is not the problem. I have tried multiple file types like .txt and .csv.

C++ Stream class failed to open the file

I write small test application in C++ to read the file data, So when I used the C++ -Stream class to read from files, it fails to open the file stream for the file name which gets passed as argument of the Stream class object.
Also I keep my file(Test.txt) in the directory where my program executable is kept and run.
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream testFile("Test.txt");
string line;
if (testFile.is_open())
{
while (getline(testFile, line))
{
line += line;
}
}
}
please also find the additional debug capture which has been captured from visual studio debugger.
If you run a program from the editor/IDE it is not always true (and generally it isn't) that the root directory for relative paths is the project directory or the one where the .exe is placed. If you run the executable from the command line placed in the directory it should work, but if you run it when the command line isn't in the directory you'll see the same error.
If you want the root directory to be the project one there is certainly an option in the editor/IDE, just surf the web or look into settings (probably run configurations)
The answer is: The file could not be opened. It is mispelled, or not existing.
But most likely it is in a different directory.
Please use a full path for the file, then you can open it. Like
ifstream testFile("c:\\temp\\Test.txt");
Then, please do not use is_open.
Simply use
if (testFile)
The ! operator for ifstream is overwritten, so this will work.
thanks all and I manage to get it working with the platform independent method. Please see the code below.
#include<fstream>
#include<string>
#include<stdlib.h>
#include<iostream>
#include <errno.h>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main(int argc, char *argv[])
{
fs::path Path = fs::current_path() / argv[1];
ifstream testFile(Path.c_str());
string line;
string str;
if (testFile.is_open())
{
for (std::string line; std::getline(testFile, line);)
{
str += line;
}
}
std::cout << str << endl;
}

Using chdir() Causes Segmentation Fault

I'm writing a batch emulator as a personal project. I'm trying to implement the cd command using chdir() from unistd.h. However, using this causes a segfault.
main.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
//Custom headers
#include "splitting_algorithm.hpp"
#include "lowercase.hpp"
#include "chdir.hpp"
//Used to get and print the current working directory
#define GetCurrentDir getcwd
using namespace std;
int main(int argc, char* argv[])
{
string command;
//Begin REPL code
while (true)
{
//Prints current working directory
cout<<cCurrentPath<<": ";
std::getline(std::cin, command);
vector<string> tempCommand = strSplitter(command, " ");
string lowerCommand = makeLowercase(string(strSplitter(command, " ")[0]));
//Help text
if(tempCommand.size()==2 && string(tempCommand[1])=="/?")
{
cout<<helpText(lowerCommand);
}
//Exit command
else if(lowerCommand=="exit")
{
return 0;
}
else if(lowerCommand=="chdir")
{
cout<<string(tempCommand[1])<<endl;
chdir(tempCommand[1]);
}
else
cout<<"Can't recognize \'"<<string(tempCommand[0])<<"\' as an internal or external command, or batch script."<<endl;
}
return 0;
}
chdir.cpp:
#include <cstdlib>
#include <string>
#include <unistd.h>
void chdir(std::string path)
{
//Changes the current working directory to path
chdir(path);
}
Strangely enough, using cout to get the path for chdir works perfectly fine. How do I fix this?
You have recursive, unterminated behaviour in Your code. This overflows the stack.
Try to insert breakpoint in void chdir(std::string path) and see what happens.
You will see that the function chdir calls itself, and in turn calls itself again, and again and... well, segmentation fault.
Also, try to see what "call stack" is in the debugger, this issue is very visible there.
You should invoke the underlying chdir function using
::chdir(path.c_str());
or you will just call your own method again.
In unistd.h, chdir is defined as:
int chdir(const char *);
So you must call it with a const char* argument or the compiler will search for another function called "chdir" which take a std::string argument and use that instead.

C++ File list with all file informations

I'm planning to make a program that would work between a folder on my computer and my NAS.
It would list all the files in both folders, then determine which file is newer, and then upload it to the other device.
I know how to upload files via FTP, but I'm stuck at the start, because I don't know how to list my files. I have looked a little at using FindFirstFile() and FindNextFile() with WIN32_FIND_DATA. This way, I can get the last write data, but this doesn't let me list subdirectories.
Do you know any easy way listing all files in a folder and its subdirectory and saving the information of every file in a list?
The easy way is to use boost::recursive_directory_iterator.
#include <boost/foreach.hpp>
#include <iostream>
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/date_time.hpp>
#include <algorithm>
#include <iterator>
#include <ctime>
using boost::filesystem::path;
using boost::filesystem::recursive_directory_iterator;
using boost::filesystem::directory_entry;
using boost::filesystem::filesystem_error;
using boost::filesystem::last_write_time;
using std::vector;
using std::cout;
using std::copy;
using std::ostream_iterator;
using std::time_t;
using boost::posix_time::from_time_t;
int main(int ac, const char **av)
{
vector<const char*> args(av+1, av+ac);
if(args.empty())
args.push_back(".");
vector<directory_entry> files;
BOOST_FOREACH(path p, args)
{
boost::system::error_code ec;
copy(recursive_directory_iterator(p, ec),
recursive_directory_iterator(),
back_inserter(files));
}
BOOST_FOREACH(const directory_entry& d, files)
{
if(exists(d.path()))
{
cout << from_time_t(last_write_time(d.path())) << " " << d.path() << "\n";
}
}
}
FindFirstFile() and FindNextFile() does let you list subdirectories. One of the members of WIN32_FIND_DATA is dwFileAttributes which will include FILE_ATTRIBUTE_DIRECTORY for a directory entry. Simply start another FindFirstFile() in that subdirector, rinse, repeat.
There is a sample on MSDN that shows how to use the FindFirstFile API, here.