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;"

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105

3 Answers3

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.

user3791372
  • 4,445
  • 6
  • 44
  • 78
  • 2
    except that you cannot easily debug it then (though it's not really clear if the op wants that) – stijn Sep 02 '14 at 13:47
  • 1
    @stijn I agree, but given such a simple program... :) – user3791372 Sep 02 '14 at 13:49
  • 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. – Mostafa Talebi Sep 02 '14 at 13:54
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"

Mr Alexander
  • 339
  • 1
  • 5
  • 13
  • 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. – Mostafa Talebi Sep 02 '14 at 13:53
  • 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? – drescherjm Sep 02 '14 at 13:59
  • 1
    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 – Mr Alexander Sep 02 '14 at 13:59
  • @Alex Patchanka And it is the reason behind my confusion. I'm doing something wrong which I'm not aware. – Mostafa Talebi Sep 02 '14 at 14:00
  • Do you know how to change directories in a cmd.exe window? Can you change directories to the folder containing your executable? – drescherjm Sep 02 '14 at 14:02
  • Well. Yes. I use cd (change-directory) for this, but I see not .exe file in my whole project and its subdirectories. Why? I don't know. You can see my updated question – Mostafa Talebi Sep 02 '14 at 14:04
  • Ok. Filing.exe is your executable, the programm that runs. filinh.cpp is your source code and it is on another directory. Just put any file (e.g. a text file) in the executable's directory and add it to the arguments and run it again. It should work – Mr Alexander Sep 02 '14 at 14:04
  • I have done this, exactly, but still to no avail – Mostafa Talebi Sep 02 '14 at 14:10
  • Probably this will help if you haven't seen it already http://stackoverflow.com/questions/1554984/c-link-debug-xxxxx-exe-not-found-or-not-built-by-the-last-incremental-link – Mr Alexander Sep 02 '14 at 14:14
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.

Zeshan Khan
  • 294
  • 2
  • 15