使用C++ ifstream读取十六进制文本文件、ofstream写入自定义二进制符号文件时仅第一行生效的问题排查
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
You're overwriting
output.txton every loop iteration
Puttingofstream 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.Incorrect loop condition with
!myfile.eof()
Checkingeof()before reading is a common pitfall. Theeofflag 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.Missing conversion for the character '1'
Your if-else chain doesn't handle the '1' case, which would leave those characters unprocessed.Range-for loop traverses the entire char array
Usingfor (char c : words)loops through all 10001 elements of your array, including the unused\0characters 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.txtonce 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 theeof()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

