0

EDIT: The following code is run through Microsoft Visual Studio 2013

I have the following script:

#include "stdafx.h"
#include <iostream>
#include <boost/filesystem.hpp>

using namespace boost::filesystem;


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

    if (argc < 2)
    {
        std::cout << "Usage: tut1 path\n";
        return 1;
    }
    std::cout << argv[1] << std::endl;
    std::cout << "File Size is: " << file_size(argv[1]) << std::endl;


    return 0;
}

But when I run it with ctrl+f5, I get this message (which is predicted by an if-condition in the code itself:

Usage: tut1 path

It seems the number of arguments is lower than 2.

Why this happens? How should I avoid this problem?

EDIT:

When I remove the following line:

std::cout << "File Size is: " << file_size(argv[1]) << std::endl;

I get the "Filing.cpp" printed on my console which means argv[0] value is Filing.cpp that further shows argv is getting the commands from command arguments of Debuger of project correctly.

But when I add the line again, I see the message "Filing.exe not found or not built by the last incremental link;"

13
  • Presumably, you're running it from an IDE. Which IDE is that? Commented Sep 2, 2014 at 13:43
  • 2
    @MikeSeymour: #include "stdafx.h" is self explanatory :-) Commented Sep 2, 2014 at 13:44
  • 1
    Yes, microsoft visual studio 2013. Commented Sep 2, 2014 at 13:44
  • 5
    Have you set the command line in the debugger? Commented Sep 2, 2014 at 13:45
  • 2
    stackoverflow.com/questions/298708/… Commented Sep 2, 2014 at 13:46

3 Answers 3

4

The easiest solution would be to open a prompt in the directory of your compiled output and call your program and pass in the string of the filename.

e.g. FileSize.exe foo.jpg

This saves messing about with project config options.

The if triggers because the application filename is considered the first argument, so argc == 1 which is less than 2, triggering the instructions.

Sign up to request clarification or add additional context in comments.

3 Comments

except that you cannot easily debug it then (though it's not really clear if the op wants that)
@stijn I agree, but given such a simple program... :)
well. I add the argument filing.cpp but it still shows the message that cannot find the file rest_of_address/Filing.exe, I even have change the Filing.cpp to ./Filing.cpp but still not change.
4

If you are running it like this the number of arguments is only one (the executables name). If you are using Visual Studio (which you propably are) and you want to add arguments, go to properties->Debugging and add the arguments you want on "Command Arguments"

9 Comments

well. I add the argument filing.cpp but it still shows the message that cannot find the file rest_of_address/Filing.exe, I even have change the Filing.cpp to ./Filing.cpp but still not change.
Remember that the default folder in Visual Studio is not the one containing the executable. It is the folder containing the solution file. Is that where you have the file that you want to read?
If you added as argument filing.cpp why does it say it cannot find Filing.exe? It doesn't make sense. filling.cpp is on another directory
@Alex Patchanka And it is the reason behind my confusion. I'm doing something wrong which I'm not aware.
Do you know how to change directories in a cmd.exe window? Can you change directories to the folder containing your executable?
|
2

If you want to run a program with arguments please run the exe file by cmd. Exe file would be in debug directory. In cmd go to path of exe file then run command like ABC.exe then arguments.

2 Comments

And how is this different to what I posted 10 mins ago? And it isn't necessarily in the "debug" directory.
Only wording is different. When i started writing answer there was not yours.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.