0

I'm trying to create a program (in c) which will have 2 options, Read and Write an NFC at Block level and I compile/execute it from my Raspberry Pi (i.e.from terminal/bash).

What I'm trying to achieve in this program is something like this :

  • ./ProgName -r /file.txt

Read the NFC and send the output to file.txt

  • ./ProgName -w /file.txt

Copy to NFC what is written in file.txt

My question is : How do I create "-r" and "-w" options?

I don't know what are they called and how are they compiled/made/created. I have a vague idea that it's something concerning argc/argv but I'm not sure.

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
  • 2
    How is the question related to `bash`? – axiac Mar 27 '18 at 12:52
  • 1
    Read about the command line arguments. They are provided as arguments to [`main()`](http://en.cppreference.com/w/c/language/main_function). – axiac Mar 27 '18 at 12:53
  • Did you mean standard handling of args in C language? The take a look [there](https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm) You have `int main( int argc, char *argv[] )`, `argc` gives you arguments length, in `argv` you can iterate over each arguments – xxxvodnikxxx Mar 27 '18 at 12:53
  • 2
    As well as using argc and argv as in the answer, you could also use a library such as getopt to manage these for you. It may be overkill for this case though. – Rup Mar 27 '18 at 12:58

1 Answers1

2

argc is the number of command line parameters (including program call) and *argv[] is a pointer to the parameters.

In other words, considering the command line ./ProgName -r /file.txt:

  • argc is 3
  • argv[0] is "./ProgName"
  • argv[1] is "-r"
  • argv[2] is "/file.txt"

A minimal program showing all command line parameters could be:

#include <stdio.h>

int main(int argc, char *argv[])
{
    for(int i = 0; i < argc; i++)
    {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
    return 0;
}
daouzli
  • 15,288
  • 1
  • 18
  • 17