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

文件内容逆序写入程序问题:文件未找到或无输出求解

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::ifstream for reading and std::ofstream for writing instead of generic std::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::ofstream defaults 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:41:04