C语言开发:如何读取指定.deck扩展名文件的技术实现问询
Got it, let's walk through building this C program step by step to meet your exact requirements for reading .deck files. I'll break down each part with clear code and explanations.
1. First: Clarify the Requirements Recap
Just to make sure we're on the same page, your program needs to:
- Accept a strict command-line format:
-d1 <name>.deck-d1is a mandatory flag—no other flags are allowed here- The target file must end with
.deck; no omitting the extension <name>can be any string of any length
- Read and process the content of the
.deckfile, which is a text file storing a deck of playing cards
2. Core Implementation Steps
2.1 Validate Command-Line Arguments
First, we need to make sure the user passes arguments correctly. Here's how to enforce the rules:
- Check that exactly 3 arguments are provided (program name +
-d1+ filename) - Verify the first argument is exactly
-d1 - Confirm the filename ends with
.deck(we'll use string manipulation for this)
2.2 Read the .deck File
Since .deck is a text file, we'll use standard C file I/O functions:
fopen()to open the file in read modefgets()to read line by line (ideal for card decks, where each line might represent a single card)- Proper error handling for cases where the file doesn't exist or can't be opened
3. Complete Working Code
Here's a fully functional example with comments explaining each part:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define DECK_EXTENSION ".deck" int main(int argc, char *argv[]) { // Step 1: Validate argument count if (argc != 3) { fprintf(stderr, "Error: Invalid number of arguments.\n"); fprintf(stderr, "Usage: %s -d1 <name>.deck\n", argv[0]); return EXIT_FAILURE; } // Step 2: Validate the -d1 flag if (strcmp(argv[1], "-d1") != 0) { fprintf(stderr, "Error: Mandatory flag '-d1' is missing or incorrect.\n"); fprintf(stderr, "Usage: %s -d1 <name>.deck\n", argv[0]); return EXIT_FAILURE; } // Step 3: Validate .deck file extension char *extension = strrchr(argv[2], '.'); if (extension == NULL || strcmp(extension, DECK_EXTENSION) != 0) { fprintf(stderr, "Error: File must have a .deck extension.\n"); fprintf(stderr, "Usage: %s -d1 <name>.deck\n", argv[0]); return EXIT_FAILURE; } // Step 4: Open and read the .deck file FILE *deck_file = fopen(argv[2], "r"); if (deck_file == NULL) { perror("Error opening file"); return EXIT_FAILURE; } printf("Successfully opened %s. Deck content:\n", argv[2]); printf("----------------------------------------\n"); char line[256]; // Adjust buffer size based on expected card line length while (fgets(line, sizeof(line), deck_file) != NULL) { // Process each line here (e.g., trim newline, validate card format) printf("%s", line); } // Cleanup fclose(deck_file); return EXIT_SUCCESS; }
4. How to Test This
- Compile the code:
gcc deck_reader.c -o deck_reader - Run with valid arguments:
./deck_reader -d1 poker_deck.deck - Test error cases to ensure validation works:
- Missing
-d1flag:./deck_reader poker_deck.deck(should throw an error) - Wrong flag:
./deck_reader -d2 poker_deck.deck(error) - No .deck extension:
./deck_reader -d1 poker_deck.txt(error) - Non-existent file:
./deck_reader -d1 nonexistent.deck(error fromperror)
- Missing
Notes for Customization
- If your
.deckfile has a specific card format (e.g., "AH" for Ace of Hearts), you can add validation logic inside thewhile(fgets(...))loop to check each line conforms to that format. - Adjust the
linebuffer size if your card entries are longer than 256 characters.
内容的提问来源于stack exchange,提问作者José Santos
相关产品推荐
相关产品推荐

