0

I have the following main method:

int main(string argf)
{

ifstream exprFile(argf);
string inExpr;
if (exprFile.is_open())
{
while ( getline(exprFile,inExpr) )
{
    //do stuff
}
exprFile.close();
}
else cout << "Unable to open file"; 

system("pause"); // to wait for user input; allows the user to see what was printed before the window closes
return 0;
}

I have run this program from the command line using the following:

  • "C:\Complete Filepath\Project2.exe" "C:\Differnt Filepath\args.txt"
  • C:\Complete Filepath\Project2.exe C:\Differnt Filepath\args.txt
  • "C:\Complete Filepath\Project2.exe" "args.txt"
  • C:\Complete Filepath\Project2.exe args.txt

The last two with args.txt being in the same directory as the executable. All four gave the "Unable to open file" result. Attemping to print the argf value before doing anything with it yielded nothing at all. A completely blank print statement.

I then went into the Visual Studio 2010 options and added all variations of the args.txt file under the arguments section there with the file in different locations as well and nothing works.

What am I doing wrong?

How are you supposed to open a file passed as an argument on the command line?

leigero
  • 3,233
  • 12
  • 42
  • 63
  • 3
    This is not a valid declaration of `main()`. See [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/) for a detailed discussion of what is allowed and what is not allowed as the definition of `main()`. – Jonathan Leffler Mar 05 '14 at 02:58

2 Answers2

2
int main ( int argc, char *argv[] )

This is correct way to get argument from main.

argc is number of arguments. argv is argument list.

Actual argument will start with index = 1. Value at index 0 will be always program name.

In your example,

"C:\Complete Filepath\Project2.exe" "C:\Differnt Filepath\args.txt"

argc = 2
argv[0] = "Project2.exe" 
argv[1] = "C:\Differnt Filepath\args.txt"
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
  • what is `int argc` in this case? What use do I have for an integer? And I'm assuming the `argv[]` is the char array (string) file location? – leigero Mar 05 '14 at 03:01
  • 1
    `argc` is the number of arguments; `argv` is a pointer to an array of pointers, each of which points to a plain-old-char-array null-terminated string. `argv[argc] == 0` so you can also detect the end of the arguments by looking at the pointers. – Jonathan Leffler Mar 05 '14 at 03:05
  • Thank you guys. I guess I was looking in the wrong place. I thought my problem was with the command line calling procedure not the argument passing. I see this is quite well covered elsewhere as well. – leigero Mar 05 '14 at 03:08
  • @JonathanLeffler Thanks for information on _argv[argc] ==0_ check. – Digital_Reality Mar 05 '14 at 03:08
0

Yay, code!

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
   ifstream exprFile;
   string inExpr;
   for( int i = 1; i < argc; i++) {  // 0 is the program name
      exprFile.open(argv[i]);
      if (exprFile.is_open()) {
         while ( getline(exprFile,inExpr) ) {
            cout << "Doing stuff on line: " << inExpr << "\n";
         }
         exprFile.close();
      }
      else cout << "Unable to open file " << argv[i];
   }
}
erik258
  • 14,701
  • 2
  • 25
  • 31