argv

Are the pointers to strings in argv modifiable? [duplicate]

放肆的年华 提交于 2019-12-01 15:59:23
问题 This question already has answers here : Is argv[n] writable? (4 answers) Closed 3 years ago . Recently (Jan 2016, in case the question persists long enough) we had the question Are the strings in argv modifiable?. In the comment section to this answer, we (@2501 and I) argued whether it is really the strings of characters (an example character being **argv ) that's modifiable or the pointers to the strings (an example pointer being *argv ). The appropriate standard quotation is from the C11

How do I sort the elements of argv in C?

独自空忆成欢 提交于 2019-12-01 12:03:37
问题 I am trying to alphabetically sort the elements of argv. The following line of code is giving me problems: qsort(argv[optind], argc - optind, sizeof(argv[optind]), sort); Specifically, the last argument is giving me trouble, the comparison function, which is given below: int sort(const void *a, const void * b) { return(strcmp( (char*)a, (char*)b )); } At the moment, it compiles fine, but I end up getting a segmentation fault when I run it. 回答1: The first argument should be argv+optind , as

How to pass command line parameters from a file

我的梦境 提交于 2019-12-01 08:28:32
I have a C program that reads command line arguments from argv. Is it possible to make a pipe to redirect the contents of a file as command line arguments to my program? Suppose I have a file arguments.dat with this content: 0 0.2 302 0 And I want my program to be called with: ./myprogram 0 0.2 302 0 I tried the following: cat arguments.dat | ./myprogram without success. With most shells, you can insert the contents of a file into a command line with $(<filename) : ./myprogram $(<arguments.dat) If your shell doesn't support that, then one of the older ways will work: ./myprogram $(cat

How do I access argv / command line options in Dart?

这一生的挚爱 提交于 2019-12-01 07:03:09
And does Dart have a getopt library? Tomas Kulich Using Options is no longer an option, use: void main(List<String> args) { print(args); } To get executable, use Platform.executable (Platform comes from dart:io) For parsing arguments passed to main, use this cool package Edit: This is no longer valid, see accepted answer above. See Options . http://api.dartlang.org/dart_io/Options.html List<String> argv = (new Options()).arguments; igor // dart 1.0 import 'dart:io'; void main(List<String> args) { String exec = Platform.executable; List<String> flags = Platform.executableArguments; Uri script =

How do I access argv / command line options in Dart?

江枫思渺然 提交于 2019-12-01 04:51:37
问题 And does Dart have a getopt library? 回答1: Using Options is no longer an option, use: void main(List<String> args) { print(args); } To get executable, use Platform.executable (Platform comes from dart:io) For parsing arguments passed to main, use this cool package 回答2: Edit: This is no longer valid, see accepted answer above. See Options . http://api.dartlang.org/dart_io/Options.html List<String> argv = (new Options()).arguments; 回答3: // dart 1.0 import 'dart:io'; void main(List<String> args)

python command line arguments in main, skip script name

旧巷老猫 提交于 2019-12-01 03:44:18
This is my script def main(argv): if len(sys.argv)>1: for x in sys.argv: build(x) if __name__ == "__main__": main(sys.argv) so from the command line I write python myscript.py commandlineargument I want it to skip myscript.py and simply run commandlineargument through commandlineargument(n) so I understand that my for loop doesn't account for this, but how do I make it do that? Since sys.argv is a list, you can use slicing sys.argv[1:] : def main(argv): for x in argv[1:]: build(x) if __name__ == "__main__": main(sys.argv) But, if you can only have one script parameter, just get it by index:

Setting argv[0] in Haskell?

一世执手 提交于 2019-12-01 03:30:38
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 name <- getProgName putStrLn $ "Hello, my name is " ++ name withProgName "other" $ do newname <- getProgName putStrLn $ "Name now set to " ++ newname putStrLn "What is your name: " -- allow time to run ps ans <- getLine putStrLn $ "Pleased to meet you, " ++ ans There is no portable way of doing this, but on Linux 2.6.9 and up the process name can be

executing a process with argc=0

痞子三分冷 提交于 2019-12-01 03:28:26
Is it possible to execute a process whose argc = 0? I need to execute a program but it is extremely important for its argc to be equal to 0. Is there a way to do that? I tried to put 2^32 arguments in the command line so that it appears as if argc = 0 but there is a maximum limit to the number of arguments. You can write a program that calls exec directly; that allows you to specify the command-line arguments (including the program name) and lack thereof. You may use linux system call execve() . int execve(const char *filename, char *const argv[], char *const envp[]); You may pass the filename

How to use python argparse with args other than sys.argv?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 02:28:28
I've been all over the documentation and it seems like there's no way to do it, but: Is there a way to use argparse with any list of strings, instead of only with sys.argv? Here's my problem: I have a program which looks something like this: # This file is program1.py import argparse def main(argv): parser = argparse.ArgumentParser() # Do some argument parsing if __name__ == '__main__': main(sys.argv) This works fine when this program is called straight from the command line. However, I have another python script which runs batch versions of this script with different commandline arguments,

Using pointers to iterate through argv[]

北慕城南 提交于 2019-12-01 02:12:33
问题 I want to use the following code, but without indexing the array with"[][]" and substitute it with pointers for (int i = 0; i < argc; i++) { for (int j = 0; argv[i][j] != '\0'; j++) { //code } } I know that you can use pointers to traverse an array, but I'm unsure how to do that with an undefined length in the second array, in this case the string from input. Since each element of argv[] can have a different length, I want to make sure that I can properly read the characters and know when