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

如何运行含测试文件的C程序?已用GCC编译,求运行命令

Hey there! Let’s walk through your questions clearly—this is a routine task when working with C programs and test inputs, so I’ll break it down with concrete examples.

1. How to Run a C Program with Test Files

First, you need two key pieces: a compiled C executable, and a test file (usually a .txt with pre-written input data). There are two main ways to use the test file, depending on how your program is set up to read input:

  • Case 1: Your program reads input via standard input (like scanf() or getchar())
    You’ll use input redirection to feed the test file’s content directly into your program as if you were typing it manually.

  • Case 2: Your program expects a test file path as a command-line argument (uses fopen() to read the file)
    You’ll pass the test file’s path directly when launching the executable, so the program can open and read it internally.

2. Exact Commands After GCC Compilation

Let’s use a concrete example: suppose your C source file is named my_program.c, and your test file is test_input.txt (in the same folder as your code).

Step 1: Compile your program with GCC

First, compile the source code into an executable. Run this in your terminal:

gcc my_program.c -o my_program

The -o my_program flag tells GCC to name the executable my_program (you can replace this with any name you like).

Step 2: Run with the test file (two scenarios)

Scenario A: Input redirection (for programs using standard input)

If your program uses scanf() or reads from stdin, run:

./my_program < test_input.txt

The < operator redirects the content of test_input.txt to your program’s standard input stream. Any output from your program will still print to the terminal.

Scenario B: Pass test file as a command-line argument

If your program is written to accept a file path (e.g., it uses argv[1] to get the test file name and fopen() to read it), run:

./my_program test_input.txt

In this case, your program needs code like this to handle the file:

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <test-file-path>\n", argv[0]);
        return 1;
    }
    FILE *test_file = fopen(argv[1], "r");
    if (!test_file) {
        perror("Failed to open test file");
        return 1;
    }
    // Read from test_file here (e.g., using fscanf(), fgets())
    fclose(test_file);
    return 0;
}

Quick Tips

  • Make sure test_input.txt is in the same directory as your executable, or use the full path (e.g., ./my_program < /home/you/tests/test_input.txt) if it’s elsewhere.
  • If you want to save the program’s output to a file instead of printing it, add output redirection: ./my_program < test_input.txt > output_results.txt.

内容的提问来源于stack exchange,提问作者Filippos Laouris

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:39:09