I've never really used argc, argv[] for reading in files however the program requirements would like me to use them. My issue is I'm reading in the files in a header file and processing it there instead of main. Is there a way to use the argc, argv[] inputs outside of main.cpp?
The header file I would like to use it in, replacing the "input.txt", etc with the argv.
Would like to use here instead of output.txt
void expression::convertToPostFix()
{
{
ofstream fout("output.txt");
stack<char> stack;
stringstream postfix;
while (!obj.empty()) {
stack.push('(');
fout << "InFix is:\t";
while (1) {
if ((obj.front() != ';') && (obj.front() != '.'))
{
const char current = obj.front();
cout << current;
fout << current;
obj.pop();
if (isspace(current)) {
}
else if (isalnum(current)) {
postfix << current;
}
else if ('(' == current) {
stack.push(current);
}
else if (isOperator(current)) {
char rightOperator = current;
while (!stack.empty() && isOperator(stack.top()) && precedence(stack.top(), rightOperator)) {
postfix << ' ' << stack.top();
stack.pop();
}
postfix << ' ';
stack.push(rightOperator);
}
else if (')' == current) {
while (!stack.empty() && '(' != stack.top()) {
postfix << ' ' << stack.top();
stack.pop();
}
stack.pop();
postfix << ' ';
}
}
else
{
obj.pop();
break;
}
}
while (!stack.empty() && '(' != stack.top()) {
postfix << ' ' << stack.top();
stack.pop();
}
stack.pop();
pfix = postfix.str();
postfix.str("");
cout << "\nPost fix is:\t" << pfix << endl << endl;
fout << "\nPost fix is:\t" << pfix << endl << endl;
}
}
}
(Would like it used here as well)
expression::expression()
{
ifix = "";
pfix = "";
last = 0;
char chr;
ifstream fin;
fin.open("input.txt");
while (!fin.eof())
{
fin >> chr;
if (!fin.eof())
{
if (chr != ' ')
{
obj.push(chr);
}
}
}
}
Answer to your question, YES it is possible to pass argc
and argv
outside of the main function.
Very simple usage program, where it uses the command line argument. It passes argv[0] as an argument to the show_usage method.
#include <iostream>
#include <string>
#include <vector>
static void show_usage(std::string name)
{
std::cerr << "Usage: " << argv[0] << " <option(s)> SOURCES"
<< "Options:\n"
<< "\t-h,--help\t\tShow this help message\n"
<< "\t-d,--destination DESTINATION\tSpecify the destination path"
<< std::endl;
}
int main(int argc, char* argv[])
{
if (argc < 3) {
show_usage(argv[0]);
return 1;
}
std::vector <std::string> sources;
std::string destination;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if ((arg == "-h") || (arg == "--help")) {
show_usage(argv[0]);
return 0;
} else if ((arg == "-d") || (arg == "--destination")) {
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
destination = argv[i++]; // Increment 'i' so we don't get the argument as the next argv[i].
} else { // Uh-oh, there was no argument to the destination option.
std::cerr << "--destination option requires one argument." << std::endl;
return 1;
}
} else {
sources.push_back(argv[i]);
}
}
return move(sources, destination);
}