C getopt multiple value

Viewed 37658

My argument is like this

./a.out -i file1 file2 file3

How can I utilize getopt() to get 3 (or more) input files? I'm doing something like this:

while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){
  case 'i':
     input = optarg; 
     break;
  ...
}

I get just the file1; how to get file2, file3?

5 Answers

The solution by GoTTimw has proven very useful to me. However, I would like to mention one more idea, that has not been suggested here yet.

Pass arguments as one string in this way.

./a.out -i "file1 file2 file3"

Then you get one string as a single argument and you only need to split it by space.

Related