You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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
    • -d1 is 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 .deck file, 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 mode
  • fgets() 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
  1. Compile the code:
    gcc deck_reader.c -o deck_reader
    
  2. Run with valid arguments:
    ./deck_reader -d1 poker_deck.deck
    
  3. Test error cases to ensure validation works:
    • Missing -d1 flag: ./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 from perror)
Notes for Customization
  • If your .deck file has a specific card format (e.g., "AH" for Ace of Hearts), you can add validation logic inside the while(fgets(...)) loop to check each line conforms to that format.
  • Adjust the line buffer size if your card entries are longer than 256 characters.

内容的提问来源于stack exchange,提问作者José Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:52:38