如何在树莓派C语言程序中实现-r和-w命令行选项?
Hey there! Those flags like -r and -w you want to use are called command-line options (or command-line arguments)—they're the standard way to pass mode instructions and file paths to terminal programs. For C programs on Raspberry Pi, the easiest and most portable way to handle these is using the getopt() function from the standard C library. Let's walk through how to build this step by step.
getopt() Basics getopt() parses command-line options following the Unix convention (single dash with short flags like -r). It takes your argc and argv parameters from main(), plus a string defining valid options. For example, "rw:" means:
-ris a standalone flag with no required argument-wrequires a file path argument (the colon:signals this)
Here's a complete skeleton that handles your -r and -w options, plus file path validation. I've added placeholders for the NFC read/write logic—you'll just need to fill those in with actual NFC library calls later:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> // For getopt() // Placeholder functions for NFC block operations void nfc_read_to_file(const char *file_path) { // Implement your block-level NFC read logic here // Example flow: Initialize NFC reader → connect to tag → read blocks → write to file FILE *output_file = fopen(file_path, "w"); if (!output_file) { perror("Failed to open output file"); exit(EXIT_FAILURE); } // Replace this with actual NFC block data fprintf(output_file, "NFC block 0: 00 11 22 33\nNFC block 1: 44 55 66 77\n"); fclose(output_file); printf("Successfully saved NFC content to %s\n", file_path); } void nfc_write_from_file(const char *file_path) { // Implement your block-level NFC write logic here // Example flow: Initialize NFC reader → connect to tag → read file content → write blocks FILE *input_file = fopen(file_path, "r"); if (!input_file) { perror("Failed to open input file"); exit(EXIT_FAILURE); } // Replace this with actual NFC write code char buffer[256]; printf("Writing content from %s to NFC blocks:\n", file_path); while (fgets(buffer, sizeof(buffer), input_file)) { printf("- %s", buffer); // Simulate writing each line as a block } fclose(input_file); printf("Finished writing to NFC tag\n"); } int main(int argc, char *argv[]) { int opt; // Parse command-line options while ((opt = getopt(argc, argv, "rw:")) != -1) { switch (opt) { case 'r': // Check if a file path was provided after -r if (optind >= argc) { fprintf(stderr, "Error: -r requires an output file path\n"); exit(EXIT_FAILURE); } nfc_read_to_file(argv[optind]); return EXIT_SUCCESS; case 'w': // optarg automatically holds the file path for -w nfc_write_from_file(optarg); return EXIT_SUCCESS; case '?': // Invalid option was provided fprintf(stderr, "Usage: %s [-r <output_file>] [-w <input_file>]\n", argv[0]); exit(EXIT_FAILURE); default: abort(); } } // If no valid options were passed fprintf(stderr, "Usage: %s [-r <output_file>] [-w <input_file>]\n", argv[0]); return EXIT_FAILURE; }
First, you'll need the NFC library for Raspberry Pi to handle actual tag communication. Install libnfc and its development files:
sudo apt update && sudo apt install libnfc-dev
Compile your program (replace nfc_prog.c with your source file name):
gcc nfc_prog.c -o ProgName -lnfc
Test the commands as you planned:
- Read NFC content to a file:
./ProgName -r /home/pi/nfc_output.txt - Write file content to NFC:
./ProgName -w /home/pi/nfc_input.txt
The placeholder functions need real NFC code. For block-level operations with libnfc, you'll use core functions like:
nfc_init()to set up the NFC contextnfc_open()to connect to your NFC readernfc_initiator_transceive_bytes()to send/receive block data (critical for MIFARE and other block-based tags)
Check the libnfc man pages (run man libnfc in the terminal) for specific examples tailored to your NFC card type.
内容的提问来源于stack exchange,提问作者Stanislav N

