I have some problem to understand which parameters would be the right once at the insert function of my <map>, maybe you can help me and explaine why?
I wanna open a file, and also save the name of the file which is given by the user over the arguments. To do this I thought it is a good soultion to make a map with a ifsteam, and a string object.
Here is the code:
int main(int argc, char** argv)
{
std::map<std::ifstream*, std::string> Dateien;
auto it_dateien = Dateien.begin();
for(size_t param = 1; param < argc; param++)
{
//No valid instance of the constructor
Dateien.insert(it_dateien,std::pair<std::ifstream*, std::string((*argv[],*argv[]));
Thanks in advance!
If I understand correctly what you want to achieve you need something like this:
int main(int argc, char** argv)
{
std::map<std::string, std::ifstream> Dateien;
for (size_t param = 1; param < argc; param++)
{
Dateien[argv[param]] = std::ifstream{ argv[param] };
}
}
I assume that you want to save in a map the name of the file you want to read, and to retreive the corresponding ifstream by doing Datein.at(NameOfFile).
For example the usage can be something like this:
std::string line;
auto& firstFile = Dateien.at(argv[1]);
if (firstFile.is_open())
{
while (std::getline(firstFile, line))
{
std::cout << line << '\n';
}
firstFile.close();
}
Related
Following this post, where I have found a temporary workaround for my other problem, I want to know if I can replace the int argc, char** argv with a std::vector<std::string> variable/object.
Consider the imaginary code:
#include <iostream>
#include <CloseLibrary>
void someFunction(int argc, char** argv){
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
int myFunc(int argc, char** argv){
someFunction(argc, argv);
return 0;
}
where the CloseLibrary is a closed library that I don't have access to the source code, and the someFunction function from that library demands the int argc, char** argv command line arguments. But for some reason I can't have double pointers char** in my code.
Here in this post something like what I need is proposed, but I don't know how to use that. Can I write the code this way:
#include <iostream>
#include <CloseLibrary>
#include <vector>
void someFunction(int argc, char** argv){
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
int myFunc("args", [](std::vector<std::string> args){
std::vector<char *> cstrs;
cstrs.reserve(args.size());
for (auto &s : args) cstrs.push_back(const_cast<char *>(s.c_str()));
someFunction(cstrs.size(), cstrs.data());
return 0;
}
Or maybe there is a more canonical way to do this? I would appreciate it if you could help me find the correct way to do this and understand the solution. Thanks for your help in advance.
P.S.1. The char* argv[] method is ok in the body of the function but not ok in the inputs. I don't know why pybind11 does this!
P.S.2. Here on pybind11 gitter, this was suggested:
void run(const std::vector<std::string>& args) {
for(auto&& e : args) std::cout << e << '\n';
}
P.S.3. Also suggested on pybind11 Gitter:
char** argv = new char*[vec.size()]; // just like malloc(sizeof(char*)*vec.size());
for (int i = 0; i < vec.size(), i++) {
argv[i] = new char[vec[i].size()];
memcpy(argv[i], vec[i].data(), vec[i].size()); // or strcpy
}
You could use the constructor which initializes the vector from a given range, with the argv parameter acts as the starting iterator and argv+argc acting as the ending iterator.
For example, I usually start my main function with:
int main( int argc, char* argv[] )
{
std::vector< std::string > args( argv, argv + argc );
for ( auto s : args )
{
std::cout << s << std::endl;
}
}
Note that this will also capture the first argument (argv[0]) which usually (but not necessarily) hold the name of the application when it is started.
In your case, you would like to do the reverse, build up a contiguous array of char* from a std::vector< std::string >. I would do something like:
std::vector< char* > rargs( args.size(), 0 ); // Initialize N nullptrs.
for ( int i=0; i<args.size(); ++i )
{
std::strcpy( rargs[i], args[i].c_str() ); // One-by-one strcpy them
}
And then you can pass them into a function accepting an argc, argv as
someFunction( rargs.size(), rargs.data() );
For what it's worth ... Taking it completely back to your original problem of not being able to use char** with pybind11, a full working example, scavenged from the pieces you posted is below. Yes, it's not pretty, but working with pointers never is.
#include <pybind11/pybind11.h>
#include <iostream>
#if PY_VERSION_HEX < 0x03000000
#define MyPyText_AsString PyString_AsString
#else
#define MyPyText_AsString PyUnicode_AsUTF8
#endif
namespace py = pybind11;
void closed_func(int argc, char** argv){
for (int i = 0; i < argc; ++i) {
std::cout << "FROM C++: " << argv[i] << std::endl;
}
}
void closed_func_wrap(py::object pyargv11) {
int argc = 0;
std::unique_ptr<char*[]> argv;
// convert input list to C/C++ argc/argv
PyObject* pyargv = pyargv11.ptr();
if (PySequence_Check(pyargv)) {
Py_ssize_t sz = PySequence_Size(pyargv);
argc = (int)sz;
argv = std::unique_ptr<char*[]>{new char*[sz]};
for (Py_ssize_t i = 0; i < sz; ++i) {
PyObject* item = PySequence_GetItem(pyargv, i);
argv[i] = (char*)MyPyText_AsString(item);
Py_DECREF(item);
if (!argv[i] || PyErr_Occurred()) {
argv = nullptr;
break;
}
}
}
// bail if failed to convert
if (!argv) {
std::cerr << "argument is not a sequence of strings" << std::endl;
return;
}
// call the closed function with the proper types
closed_func(argc, argv.get());
}
PYBIND11_MODULE(HelloEposCmd, m)
{
m.def("run", &closed_func_wrap, "runs the HelloEposCmd");
}
Which after compiling can be used as expected:
$ python - a b c d=13
>>> import HelloEposCmd
>>> import sys
>>> HelloEposCmd.run(sys.argv)
FROM C++: -
FROM C++: a
FROM C++: b
FROM C++: c
FROM C++: d=13
>>>
What I've been trying to do is...
1) to read txt files by command line argument,
2) to use strings in the txt files as arguments for the main method (or whatever method you need to invoke).
For example, there are two txt files, one of which is named character.txt and the other match.txt.
The contents of the files would be like this.
character.txt
//This comprises of six rows. Each of the rows has two string values
Goku Saiyan
Gohan Half_Saiyan
Kuririn Human
Piccolo Namekian
Frieza villain
Cell villain
match.txt
//This comprises of three rows, each of them is one string value
Goku Piccolo
Gohan Cell
Kuririn Frieza
If I use those strings without using command line, I'd declare the strings in character.txt like this.
typedef string name; //e.g. Goku
typedef string type; //e.g. Saiyan, Human, etc
Now I'm looking for how to read and send string values from txt files like the ones above, and to use them for functions inside the main method, ideally like this way.
int main(int argc, char *argv)
{
for (int i = 1; i < argc; i++) {
String name = *argv[i]; //e.g. Goku
String type = *argv[i]; //e.g. Saiyan, Human, etc
String match = * argv[i]; //Goku Piccolo
//I don't think any of the statements above would be correct.
//I'm just searching for how to use string values of txt files in such a way
cout << i << " " << endl; //I'd like to show names, types or matchs inside the double quotation mark.
}
}
Ideally, I'd like to invoke this method in this way.
According to this web site., at least I understand it is possible to use command line arguments with C++, but I cannot find any more information. I'd appreciate if you'd give any advice on it.
PS. I'm using Windows and Code Blocks.
Asuming you just want to read contents of the files and process it, you can start with this code (Without any errors checks tho). It simply gets filenames from command line and reads file contents into 2 vectors. Then you can just process these vectors as u need.
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
std::vector<std::string> readFileToVector(const std::string& filename)
{
std::ifstream source;
source.open(filename);
std::vector<std::string> lines;
std::string line;
while (std::getline(source, line))
{
lines.push_back(line);
}
return lines;
}
void displayVector(const std::vector<std::string&> v)
{
for (int i(0); i != v.size(); ++i)
std::cout << "\n" << v[i];
}
int main(int argc, char **argv)
{
std::string charactersFilename(argv[1]);
std::string matchesFilename(argv[2]);
std::vector<std::string> characters = readFileToVector(charactersFilename);
std::vector<std::string> matches = readFileToVector(matchesFilename);
displayVector(characters);
displayVector(matches);
}
to see how to use command line arguments look at this.
http://www.cplusplus.com/articles/DEN36Up4/
you cannot use the contents of the file which you have passed to your app through command line arguments. only the name of the file is passed to the app.
you should open the file using that name and read its contents. take a look at this:
http://www.cplusplus.com/doc/tutorial/files/
First the main function prototype should be
int main(int argc, char **argv)
OR
int main(int argc, char *argv[])
Second after retrieving files names in the main function you should open each file and retrieve its contents
Third Sample code
int main(int argc, char* argv[])
{
for(int i=1; i <= argc; i++) // i=1, assuming files arguments are right after the executable
{
string fn = argv[i]; //filename
cout << fn;
fstream f;
f.open(fn);
//your logic here
f.close();
}
return 0;
}
You define main prototype incorrectly. You also need std::ifstream to read files.
If you expect exactly two arguments, you may check argc and extract arguments directly:
int main(int argc, char* argv[]) {
if(argc != 3) {
std::cerr << "Usage: " << argv[0]
<< " name.txt match.txt" << std::endl;
return 1;
}
std::ifstream name_file(argv[1]);
std::ifstream match_file(argv[2]);
// ...
return 0;
}
If you expect unspecified number of files, than you need a loop and an array to save them, i.e. vector:
int main(int argc, char* argv[]) {
std::vector<std::ifstream> files;
for(int i = 1; i < argc; ++i)
files.emplace_back(argv[i]);
// ...
return 0;
}
And do not forget to check if files are openable.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp = fopen( argv[1], "r");
char line[50];
if (fp == NULL)
{
printf("File opening Unsuccessful\n");
exit(1);
}
while (fgets(line , 30 , fp) != NULL)
{
printf("%s",line);
}
fclose(fp) ;
return 0;
}
The parameter to main char* argv[] decays to char**, which is unfortunate, because it cannot be used with std::begin which only accepts arrays. The only workaround I see is to use a variable length array which is undesirable.
#include <iostream>
int main(int argc, char* argv[])
{
char* _argv[argc];
for (int i = 0; i < argc; ++i)
_argv[i] = argv[i];
for (arg : _argv)
{
std::cout << arg << " ";
}
return 0;
}
Desirably I want something like: char* _argv[] = { ... };
That is not possible, since the signature of main is fixed. You can copy the elements to a vector using:
std::vector<std::string> args {argv, argv + argc};
Then you can use std::begin on args
You can define a simple class wrapper to give you begin() and end().
struct Args {
int argc_;
char **argv_;
Args (int argc, char *argv[]) : argc_(argc), argv_(argv) {}
char ** begin () { return argv_; }
char ** end () { return argv_ + argc_; }
};
int main(int argc, char *argv[]) {
for (auto s : Args(argc, argv)) {
std::cout << s << '\n';
}
return 0;
}
The only workaround I see is to use a variable length array which is
undesirable.
The only workaround for what exactly? There is no problem. If you actually need an array of strings, use a vector as shown in TNAs answer; I doubt you need to modify the parameters though.
If you just need to iterate through it, there is no need to copy all the pointers.
for (auto str : boost::make_iterator_range(argv, argv + argc))
std::cout << str;
Or
for (auto ptr = argv; ptr != argv + argc; ++ptr)
std::cout << *ptr << '\n';
The parameter to main char* argv[] decays to char**, which is
unfortunate, because it cannot be used with std::begin which only
accepts arrays.
There is no other way. The amount of command line parameters is not known at compile time, and this method of passing them (instead of using two pointers-to-pointers) is historically motivated.
I am having some trouble understanding the process of creating this vector of structs. I am passing files into the program as parameters, and using their file locations to create a struct that contains this information. Is there a way to create the struct and add it to the vector using struct functions?
struct fileDetails
{
string filePath;
string fileName;
string fileExt;
void setFileDetails(char** path);
};
void fileDetails::setFileDetails(char** path)
{
filePath = path.substr(0, path.find_last_of('\\'));
fileExt = path.substr(path.find_last_of(".") + 1);
fileName = path.substr(path.find_last_of('\\') + 1);
fileName = fileName.substr(0, fileName.find_last_of('.'));
}
int main(int argc, char** argv[])
{
vector<fileDetails> fileList;
fileDetails fDet;
for (int i = 0; i < argc; i++)
{
fDet.setFileDetails(argv[i]);
fileList.push_back(fDet);
}
}
Or what would be the better approach to this? I tried to do this using this format, but I am getting errors.
You have two main errors.
First of all, char** does not have any substr member function. What you probably meant was to use std::string instead:
void setFileDetails(std::string path);
// ^^^^^^^^^^^
And second, you have an unnecessary * in argv:
int main(int argc, char** argv[])
// ^
If you fix both, the program will compile just fine.
I need to save all arguments to a vector or something like this. I'm not a programmer, so I don't know how to do it, but here's what I've got so far. I just want to call a function system to pass all arguments after.
#include "stdafx.h"
#include "iostream"
#include "vector"
#include <string>
using namespace std;
int main ( int argc, char *argv[] )
{
for (int i=1; i<argc; i++)
{
if(strcmp(argv[i], "/all /renew") == 0)
{
system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\"");
}
else
system("c:\\windows\\system32\\ipconfig.exe"+**All Argv**);
}
return 0;
}
i need to save all arguments to a vector or something
You can use the range constructor of the vector and pass appropriate iterators:
std::vector<std::string> arguments(argv + 1, argv + argc);
Not 100% sure if that's what you were asking. If not, clarify.
To build string with all argument concatenated and then run a command based on those arguments, you can use something like:
#include <string>
using namespace std;
string concatenate ( int argc, char* argv[] )
{
if (argc < 1) {
return "";
}
string result(argv[0]);
for (int i=1; i < argc; ++i) {
result += " ";
result += argv[i];
}
return result;
}
int main ( int argc, char* argv[] )
{
const string arguments = concatenate(argc-1, argv+1);
if (arguments == "/all /renew") {
const string program = "c:\\windows\\system32\\ipconfig.exe";
const string command = program + " " + arguments;
system(command.c_str());
} else {
system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\"");
}
}