文件内容逆序写入程序问题:文件未找到或无输出求解
Fixing File Reversal with Stack: Resolving "File Not Found" & No Output Issues
Hey there! Let's work through your problem of reading a file into a stack and writing its reversed content to another file. From your partial code, I can point out several common mistakes that are likely causing the file not found error or lack of output, plus share a corrected version of your program.
Common Issues in Your Original Code
- Missing explicit file open modes: When using
std::fstream, you need to specify whether you're opening the file for reading, writing, or both. Without this, the file might not open correctly. - No file existence checks: You didn't add checks to confirm if the input file opened successfully—if it fails, the rest of the code runs without any data, leading to no output.
- Incomplete character reading: Using the
>>operator skips whitespace (spaces, newlines, tabs), which would break the reversal of the full file content. - Unspecified output file handling: The output file wasn't set up with the right mode to create/overwrite content, and there's no guarantee the buffer is flushed to disk.
Corrected Full Code
#include "stdafx.h" #include <iostream> #include <stack> #include <fstream> #include <string> int main() { std::stack<char> charStack; std::ifstream inputFile; // Use ifstream for reading only (safer than generic fstream) std::ofstream outputFile; // Use ofstream for writing only std::string inputFileName, outputFileName; // Get input file name from user std::cout << "Enter the name of the file you want to reverse: "; std::getline(std::cin, inputFileName); // Open input file in read mode inputFile.open(inputFileName); // Check if input file opened successfully if (!inputFile.is_open()) { std::cerr << "Error: Could not open input file '" << inputFileName << "'!\n"; return 1; // Exit with error code } // Read every character (including whitespace and newlines) into the stack char c; while (inputFile.get(c)) { charStack.push(c); } // Close input file since we're done reading inputFile.close(); // Get output file name from user std::cout << "Enter the name of the output file: "; std::getline(std::cin, outputFileName); // Open output file in write mode (creates file if it doesn't exist, overwrites if it does) outputFile.open(outputFileName); if (!outputFile.is_open()) { std::cerr << "Error: Could not open output file '" << outputFileName << "'!\n"; return 1; } // Pop characters from stack and write to output file (this reverses the content) while (!charStack.empty()) { outputFile.put(charStack.top()); charStack.pop(); } // Close output file to ensure all data is flushed to disk outputFile.close(); std::cout << "File reversed successfully! Check '" << outputFileName << "'\n"; return 0; }
Key Improvements Explained
- Specialized stream types: Used
std::ifstreamfor reading andstd::ofstreamfor writing instead of genericstd::fstream—this makes the code clearer and avoids accidental mode issues. - Existence checks: Added
is_open()checks for both files, with clear error messages to tell you exactly what went wrong. - Full character reading:
inputFile.get(c)reads every single character, including spaces, newlines, and tabs, so your reversed file will match the original's structure perfectly. - Proper output mode:
std::ofstreamdefaults to write mode (ios::out | ios::trunc), which creates a new file or overwrites an existing one—no unexpected behavior here. - Explicit file closing: While streams close automatically when they go out of scope, explicitly closing them ensures data is flushed immediately, preventing lost content.
内容的提问来源于stack exchange,提问作者Kenneth Freeman
相关产品推荐
相关产品推荐

