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

使用C++ ifstream读取十六进制文本文件、ofstream写入自定义二进制符号文件时仅第一行生效的问题排查

Fixing Your Hex-to-Binary-Text Converter

Let's walk through the issues in your code and fix them to get the 7 lines of output you expect:

Key Problems in Your Current Code

  1. You're overwriting output.txt on every loop iteration
    Putting ofstream outfile; outfile.open("output.txt"); inside the while loop means you're opening the file from scratch each time. The default open mode (ios::out) clears the file's existing content every time, so only the last iteration's output would normally stay—but in your case, other issues are making it stop earlier.

  2. Incorrect loop condition with !myfile.eof()
    Checking eof() before reading is a common pitfall. The eof flag only gets set after a failed read attempt, so this loop can run an extra time when you reach the end of the file, leading to unexpected behavior.

  3. Missing conversion for the character '1'
    Your if-else chain doesn't handle the '1' case, which would leave those characters unprocessed.

  4. Range-for loop traverses the entire char array
    Using for (char c : words) loops through all 10001 elements of your array, including the unused \0 characters at the end. This can add unnecessary empty content to your output.

Corrected Code

#include <iostream>
#include <fstream>
#include <cstring> // For strlen()
using namespace std;

int main() {
    ifstream myfile("test.txt");
    ofstream outfile("output.txt"); // Open output file ONCE, outside the loop
    char words[10001] = {'\0'};

    if (myfile.is_open() && outfile.is_open()) {
        // Read words one by one until end of file (automatically stops on failure)
        while (myfile >> words) {
            // Loop only through the actual characters in the string
            int len = strlen(words);
            for (int i = 0; i < len; ++i) {
                char c = words[i];
                if (c == '0') outfile << "----";
                else if (c == '1') outfile << "---#"; // Fixed missing case
                else if (c == '2') outfile << "--#-";
                else if (c == '3') outfile << "--##";
                else if (c == '4') outfile << "-#--";
                else if (c == '5') outfile << "-#-#";
                else if (c == '6') outfile << "-##-";
                else if (c == '7') outfile << "-###";
                else if (c == '8') outfile << "#---";
                else if (c == '9') outfile << "#--#";
                else if (c == 'a') outfile << "#-#-";
                else if (c == 'b') outfile << "#-##";
                else if (c == 'c') outfile << "##--";
                else if (c == 'd') outfile << "##-#";
                else if (c == 'e') outfile << "###-";
                else if (c == 'f') outfile << "####";
            }
            outfile << endl; // Add a newline after each hex string's conversion
        }
        myfile.close();
        outfile.close();
    } else {
        cerr << "Failed to open one or more files!" << endl;
    }
    return 0;
}

What Changed & Why

  • Opened output.txt once outside the loop: This avoids overwriting the file each time and keeps it open for all writes, which is also more efficient.
  • Used while (myfile >> words) as the loop condition: This properly stops the loop as soon as a read fails (like when reaching the end of the file), eliminating the eof() bug.
  • Added the '1' conversion: Fixed the missing case to match your specified rules.
  • Looped with strlen(words): Only processes the actual characters in the current string, ignoring unused parts of the char array.
  • Added outfile << endl;: Ensures each hex string's converted output gets its own line, matching your expectation of 7 lines.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 11:27:44