dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: main function with arguments

  1. #1
    Join Date
    Nov 2011
    Posts
    12

    main function with arguments

    Hello,
    I have tried to used the main function with arguments using visual studio as follows:

    Code:
    int main(int argc, char* argv[]) {
        std::string projectPathAndFile;
    
        if (argc > 1)
            projectPathAndFile = argv[1];
    
        std::cout<< "Program argc's:" << argc << std::endl;
    
        try {
            RunProgram::runProgram(projectPathAndFile);
        }
    
        return 0;
    }
    Why it does not recognize the directory and the file during debugging?

    Thanks.
    Last edited by 2kaud; October 13th, 2018 at 02:54 AM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,805

    Re: main function with arguments

    Are you setting the debug arguments in visual studio via the debug tab under project properties before pressing F5?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,258

    Re: main function with arguments

    Quote Originally Posted by laephy View Post
    Hello,
    I have tried to used the main function with arguments using visual studio as follows:
    Code:
    int main(int argc, char* argv[])
    {
       std::string projectPathAndFile;
       if(argc>1)
           projectPathAndFile=argv[1];
       std::cout<< "Program argc's:" << argc << std::endl;
    
       try
       {
          RunProgram::runProgram(projectPathAndFile);
       }
    
       return 0;
    }
    Why it does not recognize the directory and the file during debugging?
    You are using try block without corresponding catch. Does your code compile?
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: main function with arguments

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#']

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: main function with arguments

    Why it does not recognize the directory and the file during debugging?
    How are you specifying the command arguments when debugging?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  6. #6
    Join Date
    Feb 2017
    Posts
    360

    Re: main function with arguments

    Quote Originally Posted by laephy View Post
    Why it does not recognize the directory and the file during debugging?
    If your program prints the path you entered the issue must lie with RunProgram::runProgram. Do you have the source?

    Does your program do the expected in release mode but not in debug mode? Have you tried to enter the full path to the file?

  7. #7
    Join Date
    Nov 2011
    Posts
    12

    Re: main function with arguments

    Of course I am using the catch, only I forget to put here. Sorry

  8. #8
    Join Date
    Nov 2011
    Posts
    12

    Re: main function with arguments

    Quote Originally Posted by wolle View Post
    If your program prints the path you entered the issue must lie with RunProgram::runProgram. Do you have the source?

    Does your program do the expected in release mode but not in debug mode? Have you tried to enter the full path to the file?
    Of course I have the source for RunProgram::runProgram and it works if I use

    Code:
    fopen("C:\Development\Dll\Input.xml");

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,258

    Re: main function with arguments

    Quote Originally Posted by laephy View Post
    Of course I have the source for RunProgram::runProgram and it works if I use

    Code:
    fopen("C:\Development\Dll\Input.xml");
    Did you probably mean
    Code:
    fopen("C:\\Development\\Dll\\Input.xml");
    Last edited by VictorN; October 14th, 2018 at 10:18 AM.
    Victor Nijegorodov

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: main function with arguments

    ... and the answer to post #5 ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  11. #11
    Join Date
    Nov 2011
    Posts
    12

    Re: main function with arguments

    Quote Originally Posted by Arjay View Post
    Are you setting the debug arguments in visual studio via the debug tab under project properties before pressing F5?
    I am not sure if I set the correct debug arguments.

    Is all correct or not?
    Attached Images Attached Images  

  12. #12
    Join Date
    Nov 2011
    Posts
    12

    Re: main function with arguments

    Quote Originally Posted by 2kaud View Post
    How are you specifying the command arguments when debugging?
    I press F5 and it say argv = 1 and therefore ignore

    Code:
    if (argc > 1)
            projectPathAndFile = argv[1];
    I have read that one can specify the arguments by command prompt but how can I specify the arguments using F5?

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,258

    Re: main function with arguments

    Quote Originally Posted by laephy View Post
    I press F5 and it say argv = 1 and therefore ignore

    Code:
    if (argc > 1)
            projectPathAndFile = argv[1];
    I have read that one can specify the arguments by command prompt but how can I specify the arguments using F5?
    if you are not banned by Google:
    https://www.google.de/search?newwind....0.eVUupWdEKEc
    Victor Nijegorodov

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: main function with arguments

    Quote Originally Posted by laephy View Post
    I am not sure if I set the correct debug arguments.

    Is all correct or not?
    No - as you haven't specified any Command Arguments. That is why argc is 1. You specify what you would type on the command line following the program name in Command Arguments.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)