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.

4
  • 2
    How is the question related to bash? Commented Mar 27, 2018 at 12:52
  • 1
    Read about the command line arguments. They are provided as arguments to main(). Commented Mar 27, 2018 at 12:53
  • Did you mean standard handling of args in C language? The take a look there You have int main( int argc, char *argv[] ), argc gives you arguments length, in argv you can iterate over each arguments Commented Mar 27, 2018 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. Commented Mar 27, 2018 at 12:58

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! This is what i was looking for !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.