Questions tagged [argv]

Argument vector containing the arguments passed in from the command line when starting a program. Used in the main method, in conjunction with argc.

argv stands for arguments vector, and represents the arguments given to the program, or main, in a command-line. It is used in conjunction with , the number of arguments given to a program.

Keep in mind that the first value in argv contains the program name.

Example usage:

int main(int argc, char **argv) {
    /* Some code involving command-line arguments from argv */
}

Notable Stack Overflow questions:

1247 questions
10
votes
2 answers

Why does main(int argc, char* argv[]) take two argument?

I always thought that argc was required to mark the end of argv but I just learned that argv[argc] == NULL by definition. Am I right in thinking that argc is totally redundant? If so, I always thought C made away with redundancy in the name of…
elik
  • 180
  • 1
  • 9
10
votes
6 answers

Using argv in C?

For an assignment, I am required to have command line arguments for my C program. I've used argc/argv before (in C++) without trouble, but I'm unsure if C style strings are affecting how this works. Here is the start of my main: int main(int argc,…
Joe
  • 125
  • 1
  • 1
  • 5
10
votes
3 answers

Can I skip a whole file with the <> operator?

The following Perl code has an obvious inefficiency; while (<>) { if ($ARGV =~ /\d+\.\d+\.\d+/) {next;} ... or do something useful } The code will step through every line of the file we don't want. On the size of files this particular script is…
Chris Huang-Leaver
  • 6,059
  • 6
  • 41
  • 67
10
votes
5 answers

Process argc and argv outside of main()

If I want to keep the bulk of my code for processing command line arguments out of main (for organization and more readable code), what would be the best way to do it? void main(int argc, char* argv[]){ //lots of code here I would like to move…
Joey Huggins
  • 125
  • 1
  • 1
  • 6
9
votes
2 answers

Setting argv[0] in Haskell?

Is there a way to set argv[0] in a Haskell program (say, one compiled with ghc)? I found the getProgName and withProgName functions in System.Environment, but it doesn't seem to change what ps reports (Ubuntu). import System.Environment main = do…
ErikR
  • 51,541
  • 9
  • 73
  • 124
9
votes
4 answers

Can argv[0] contain an empty string?

In any C program, the command line argument argv[0] points to the name used to invoke the program. Is there any circumstance in which it will point to an empty string ""? An example code snippet for such a case would be a good reference.
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
9
votes
3 answers

Clear sys.argv in python

When user specify a password in MySQL cli, aka -pXXXXXX, the password parameter is replaced to -p****** in argv array. So when someone check the process list with ps, they can't see the password. How can I do the same in Python? This doesn't work…
daisy
  • 22,498
  • 29
  • 129
  • 265
9
votes
2 answers

create argc argv in the code

Hi very newbie question but I just can't figure it out: I have a function named bar class foo { public: bool bar(int argc, char** argv); } argv is supposed to contain "--dir" and "/some_path/" How do I create argv and argc so that I…
KKsan
  • 147
  • 1
  • 1
  • 8
9
votes
10 answers

convert string to argv in c++

I have an std::string containing a command to be executed with execv, what is the best "C++" way to convert it to the "char *argv[]" that is required by the second parameter of execv()? To clarify: std::string cmd = "mycommand arg1 arg2"; char…
aaronstacy
  • 6,189
  • 13
  • 59
  • 72
9
votes
3 answers

How to Get the Path of the executing frozen script

If you are running a frozen python script (frozen using py2exe) from a directory and drive different from where the script is present, what is the best way to determine the path of the executing script? Few solutions I have…
Abhijit
  • 62,056
  • 18
  • 131
  • 204
8
votes
1 answer

How do you pass the string "*.*" to ruby as a command line parameter?

code: #test_argv.rb puts "length: #{ARGV.length} " ARGV.each do |a| puts "Argument: #{a}" end If I supply the string "*.*" (with or without quotes) when I call the above, I get the following output: C:\test>test_argv *.* length: 5 …
davej
  • 1,350
  • 5
  • 17
  • 34
8
votes
3 answers

Why do we pass the command name twice to execve, as a path and in the argument list?

I have a program written by my professor that prints the working directory (pwd) by using execve(), but I don't understand the parameters. pid_t pid = fork(); if(pid <0) perror(NULL); else if(pid == 0) { char*argv[] = {"pwd",NULL}; …
Andrei Manolache
  • 766
  • 1
  • 8
  • 17
8
votes
4 answers

What does envp stand for?

I wonder what the abbreviation envp stands for, for example here: int main(int argc, char **argv, char **envp); I also wonder what the v in argv initially stood for. Was the v for "value"? Or maybe "vector"?
nngm
  • 129
  • 1
  • 1
  • 8
8
votes
1 answer

Argc and Argv in Bash

I written the following script which gets name of a file and then assemble and link the file. But it doesn't work. What is the problem with it? EXPECTED_ARGS=2 if [ $# -ne $EXPECTED_ARGS ] then echo "[+] Assembling with Nasm." nasm…
user7995295
8
votes
2 answers

How to check if an argv refers to an existing file in python2.7?

I know how to check whether required arguments are given on the command line. But how do I check whether the given argument on the command line actually refers to an existing file in the folder I'm running the code in? I'm trying to incorporate this…
Marlen
  • 91
  • 2