0

I'm making a university project which is supposed to read a table from stdin, apply some changes to it and print to stdout. Here's how the program should be run:

./main [delimiter] [function] <file1.txt >file2.txt

[delimiter] is the character that will divide the cells in the resulting table, defined in the body;

[function] is the function that will be performed to modify rows or columns, defined in the body.

So my question is, how can I read the [delimiter] and [function] from the terminal so that I can use them accordingly in the body of the program?

harvey
  • 5
  • 2

1 Answers1

0

A C program generally has a main function with a signature like:

int main (int argc, char *argv[])

where argc is an integer that tells you how many things are in the array, argv, and argv is an array of arguments starting with the name of the program (at index 0) and including all the options and parameters that you specify when you invoke the program. Since parsing arguments is something that so many programs have to do, there are various libraries that simplify the task. You can find a number of them listed in the question Parsing command-line arguments in C?.

Parsing the arguments yourself really isn't difficult, though, especially if your program expects the arguments in a particular sequence. Just loop over the entries in argv and read the strings.

Caleb
  • 124,013
  • 19
  • 183
  • 272