Thread: Problem with argc

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Problem with argc

    Why does an if stament that checks to see if argc is less than 4 get entered in my program?? I passed exactly 4 arguments. The three arguments after the program name all are surrounded with quotes. argv[0] is the program name, argv[1] is the input file path, argv[2] is a list of search strings, and argv[3] is a list of replace strings.

    Here is the if statement that is being entered:

    Code:
    if (argc < 4) {
       cerr<< "Error! You must pass the path to the file which contains\n"
                  "stuff you want to replace as the first argument to the program,\n"
                  "after the program name, and also pass two other arguments: the\n"
                  "list of search strings, and the list of replace strings, in that\n"
                  "order.\n\nPress Enter to access Help and quit.\n\n";
       cin.get();
       help();
       return 1;
    }
    And here is the arguments I pass to the program:

    Code:
    ReplaceStrsInLines "/home/USERNAME/Documents/C_html4_elements.h" "[string;desc]" "[C_html_element;element]"
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    And argv[0] ReplaceStrsInLines
    And argv[1] "/home/USERNAME/Documents/C_html4_elements.h"
    And argv[2] "[string;desc]"
    and argv[3] "[C_html_element;element]"

    I don't know how you add but that adds up to 4 arguments to me. And since 4 is not less than 4?

    Jim

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    And argv[0] ReplaceStrsInLines
    And argv[1] "/home/USERNAME/Documents/C_html4_elements.h"
    And argv[2] "[string;desc]"
    and argv[3] "[C_html_element;element]"

    I don't know how you add but that adds up to 4 arguments to me. And since 4 is not less than 4?

    Jim
    Precisely.
    That's why I don't understand why the if statement which checks to see if argc < 4 is entered when I have exactly 4 arguments.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Here is the entire output when I run it with the above arguments:

    Thank you for running ReplaceStrsInLines.
    This program was created for the purpose of reading a file,
    finding every line of the file which includes each search string
    passed to the program, and replacing the bits of text in those lines
    with the replace strings you pass to the program.

    Please press Enter to continue.

    Processing...please wait.
    Error! You must pass the path to the file which contains
    stuff you want to replace as the first argument to the program,
    after the program name, and also pass two other arguments: the
    list of search strings, and the list of replace strings, in that
    order.

    Press Enter to access Help and quit.


    sh: desc]: not found
    sh: element: not found

    Press Enter to continue.
    Hmm...and I just realized I forgot to output help there. I just call the function, but I dont output the return value.

    Also note that I don't know what that last part of the output is. I don't have it in my source code.
    It must be an error message outputted by a system function which is triggered by some event.
    Last edited by Programmer_P; 02-21-2011 at 01:20 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Have you considered using a debugger?

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by kmdv View Post
    Have you considered using a debugger?
    I used a debugger on it, but it didn't detect any problems.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So you're trying to tell us that you set a breakpoint on the line with if (argc <4) and ran it in the debugger, checked the values of argc and argv, they were as you expected above, and yet you entered that block?

    Not believing it.

  8. #8
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Programmer_P View Post
    Why does an if stament that checks to see if argc is less than 4 get entered in my program?? I passed exactly 4 arguments.
    Weird.

    Code:
     1 #include <iostream>
     2
     3 int main(int argc, char** argv)
     4 {
     5     if (argc < 4)
     6         std::cout << "Less than 4 arguments" << std::endl;
     7
     8     return 0;
     9 }
    Output:
    Code:
    stuff$ a.out 2
    Less than 4 arguments
    stuff$ a.out 2 3
    Less than 4 arguments
    stuff$ a.out 2 3 4
    stuff$ a.out 2 3 4 5
    stuff$
    Works as expected. I guess I'll try something closer to your code to see if that somehow makes a difference:

    Code:
     1 #include <iostream>
     2 using namespace std;
     3
     4 int main(int argc, char** argv)
     5 {
     6     if (argc < 4) {
     7        cerr<< "Error! You must pass the path to the file which contains\n"
     8                   "stuff you want to replace as the first argument to the program,\n"
     9                   "after the program name, and also pass two other arguments: the\n"
    10                   "list of search strings, and the list of replace strings, in that\n"
    11                   "order.\n\nPress Enter to access Help and quit.\n\n";
    12        cin.get();
    13        //help();
    14        return 1;
    15     }
    16
    17     return 0;
    18 }
    Output:
    Code:
    stuff$ g++ argc2.cpp -o ReplaceStrsInLines
    stuff$ ReplaceStrsInLines "/home/USERNAME/Documents/C_html4_elements.h" "[string;desc]" "[C_html_element;element]"
    stuff$
    That's what I thought. Could you do some experimentation and maybe post a complete program in the form of a short version of your code that also has the problem you described?

  9. #9
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by rags_to_riches View Post
    So you're trying to tell us that you set a breakpoint on the line with if (argc <4) and ran it in the debugger, checked the values of argc and argv, they were as you expected above, and yet you entered that block?

    Not believing it.
    Hey only a week or two ago at work I saw gdb enter "if (shouldBeInMap) { (stuff) }" when gdb also said shouldBeInMap was false. I don't think the executable was actually doing that, but the debugger said the program was in there.

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by rags_to_riches View Post
    So you're trying to tell us that you set a breakpoint on the line with if (argc <4) and ran it in the debugger, checked the values of argc and argv, they were as you expected above, and yet you entered that block?

    Not believing it.
    Well, no, I didn't set any breakpoints until after reading your post.
    Then I did, and the debugger says argc is 3, but that still doesn't make any sense seeing as I passed 4 arguments to the program, not 3.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Mozza314 View Post
    Weird.

    Code:
     1 #include <iostream>
     2
     3 int main(int argc, char** argv)
     4 {
     5     if (argc < 4)
     6         std::cout << "Less than 4 arguments" << std::endl;
     7
     8     return 0;
     9 }
    Output:
    Code:
    stuff$ a.out 2
    Less than 4 arguments
    stuff$ a.out 2 3
    Less than 4 arguments
    stuff$ a.out 2 3 4
    stuff$ a.out 2 3 4 5
    stuff$
    Works as expected. I guess I'll try something closer to your code to see if that somehow makes a difference:

    Code:
     1 #include <iostream>
     2 using namespace std;
     3
     4 int main(int argc, char** argv)
     5 {
     6     if (argc < 4) {
     7        cerr<< "Error! You must pass the path to the file which contains\n"
     8                   "stuff you want to replace as the first argument to the program,\n"
     9                   "after the program name, and also pass two other arguments: the\n"
    10                   "list of search strings, and the list of replace strings, in that\n"
    11                   "order.\n\nPress Enter to access Help and quit.\n\n";
    12        cin.get();
    13        //help();
    14        return 1;
    15     }
    16
    17     return 0;
    18 }
    Output:
    Code:
    stuff$ g++ argc2.cpp -o ReplaceStrsInLines
    stuff$ ReplaceStrsInLines "/home/USERNAME/Documents/C_html4_elements.h" "[string;desc]" "[C_html_element;element]"
    stuff$
    That's what I thought. Could you do some experimentation and maybe post a complete program in the form of a short version of your code that also has the problem you described?
    Sure.
    Give me a few minutes to put together a shorter version which has this problem.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #12
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Programmer_P View Post
    Well, no, I didn't set any breakpoints until after reading your post.
    Then I did, and the debugger says argc is 3, but that still doesn't make any sense seeing as I passed 4 arguments to the program, not 3.
    Check the values of argv[0], argv[1], argv[2]. It's highly unusual, but I think it may be possible to run a program without passing it its own name as argv[0]. If your ide has this unusual but possible behaviour, that could explain it.

    Try this program:

    Code:
     1 #include <iostream>
     2 using namespace std;
     3
     4 int main(int argc, char** argv)
     5 {
     6     for (int i = 0; argv[i] != NULL; ++i)
     7         cout << argv[i] << ' ';
     8
     9     cout << endl;
    10
    11     return 0;
    12 }

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Mozza314 View Post
    Check the values of argv[0], argv[1], argv[2]. It's highly unusual, but I think it may be possible to run a program without passing it its own name as argv[0]. If your ide has this unusual but possible behaviour, that could explain it.

    Try this program:

    Code:
     1 #include <iostream>
     2 using namespace std;
     3
     4 int main(int argc, char** argv)
     5 {
     6     for (int i = 0; argv[i] != NULL; ++i)
     7         cout << argv[i] << ' ';
     8
     9     cout << endl;
    10
    11     return 0;
    12 }
    Hmm...good idea.
    I added the following for loop to my code, before that if statement which checks to see if argc < 4:

    Code:
    for (int i = 0; argv[i] != NULL; ++i)
            cout<< "argv[" << i << "] is:\n" << argv[i] << '\n' <<endl;
    Now the output was:

    PROGRAM_INTRODUCTION

    Please press Enter to continue.

    Processing...please wait.
    argv[0] is:
    /home/USERNAME/Documents/Programming_Projects/ReplaceStrsInLines/bin/Debug/ReplaceStrsInLines

    argv[1] is:
    /home/USERNAME/Documents/C_html4_elements.h

    argv[2] is:
    [string

    OUTPUT_OF_IF_STATEMENT
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The semicolon is obviously being interpreted by the shell as the end of that command. Try escaping it.

    Spending a few hours to learn to use the debugger will save you (and us) countless hours of grief and frustration.

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by rags_to_riches View Post
    The semicolon is obviously being interpreted by the shell as the end of that command. Try escaping it.

    Spending a few hours to learn to use the debugger will save you (and us) countless hours of grief and frustration.
    Why would it be doing that?
    I thought the shell uses the space character as the end of an argument, except for the last argument, though it ignores spaces inside of quotes.
    And I'm assuming by "escaping" it you mean pass this to the program:

    Code:
    ReplaceStrsInLines "/home/USERNAME/Documents/C_html4_elements.h" "[string\;desc]" "[C_html_element\;element]"
    ?

    Also note that the program worked ok with the following arguments:

    Code:
    ReplaceStrsInLines "/home/USERNAME/Documents/C_html4_elements.h" "[map<string, vector_str_t>;supported_attrs_list]" "[map<S_browser, TYPE_attrs>]"
    though there is a semicolon in argv[2]. So why would it do that if the shell is interpreting the semicolon as the end of an argument??

    Side note: I do use the debugger. However, it wasn't very helpful with this problem, was it?
    Last edited by Programmer_P; 02-21-2011 at 05:53 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. problem in calling DLL
    By bhagwat_maimt in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 10:43 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM