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

C++如何实现从文件读取输入并写入输出文件 支持终端命令调用

问题说明

该代码的运行逻辑为:输入一个初始组合以及作为目标的另一组组合,示例如下:
1
前两行是初始组合,接下来两行是目标组合,程序会输出从初始组合转换到目标组合所需的最少移动步数。
改造需求为让程序支持终端运行:传入input.txt作为输入文件,计算结果保存为output.txt,可通过终端命令app.exe input.txt output.txt启动程序。当前代码原有功能运行正常,但文件读写逻辑存在问题。


现有代码问题梳理

  • 硬编码输入文件名为input.txt,没有读取命令行传入的输入、输出文件参数
  • 混用C风格文件操作和C++标准输入流cin,代码中提前用fopen读取了整个输入文件,文件指针已走到末尾,后续State::read调用cin无法读取到有效输入
  • 计算结果直接输出到cout,没有写入指定的输出文件
  • 存在拼写错误:比较函数的参数名rigth应为right

修正后完整代码

#include <iostream>
#include <fstream>
#include <queue>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct State {
    char a[2][4];

    // 改为支持从任意输入流读取
    static State read(istream& in) {
        State s;
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 4; ++j) {
                in >> s.a[i][j];
            }
        }
        return s;
    }

    State shift(int di, int dj) const {
        State next = *this;

        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 4; ++j) {
                if (next.a[i][j] == '#') {
                    int ni = di + i;
                    int nj = dj + j;

                    if (ni >= 0 && ni < 2 && nj >= 0 && nj < 4) {
                        swap(next.a[i][j], next.a[ni][nj]);
                    }
                    return next;
                }
            }
        }
        return *this; // 补充缺失的返回值,避免编译警告
    }
};

// 修正参数名拼写错误
int compare(const State& left, const State& right) {
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (left.a[i][j] != right.a[i][j]) {
                return left.a[i][j] - right.a[i][j];
            }
        }
    }
    return 0;
}

bool operator < (const State& left, const State& right) {
    return compare(left, right) < 0;
}
bool operator == (const State& left, const State& right) {
    return compare(left, right) == 0;
}

int main(int argc, char** argv)
{
    if (argc < 3) {
        perror("Not enough arguments, usage: app.exe input.txt output.txt");
        return 1;
    }

    // 读取命令行传入的输入输出路径
    ifstream input_file(argv[1]);
    if (!input_file.is_open()) {
        perror("Failed to open input file");
        return EXIT_FAILURE;
    }
    ofstream output_file(argv[2]);
    if (!output_file.is_open()) {
        perror("Failed to open output file");
        return EXIT_FAILURE;
    }

    // 从输入文件读取状态
    State start = State::read(input_file);
    State finish = State::read(input_file);

    map<State, int> len;
    queue<State> q;
    len[start] = 0;
    q.push(start);

    int result = -1;
    while (!q.empty()) {
        State cur = q.front();
        q.pop();

        if (cur == finish) {
            result = len[cur];
            break;
        }

        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (i * i + j * j == 1) {
                    State next = cur.shift(i, j);
                    if (len.count(next) == 0) {
                        len[next] = len[cur] + 1;
                        q.push(next);
                    }
                }
            }
        }
    }

    // 结果写入输出文件
    output_file << result;
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 04:06:03