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

能否为已编译的.o文件添加等效于getchar()的暂停功能?

Fixing the "Instantly Closing EXE" Issue Without Source Code

Got it, let's sort out this frustrating problem where your compiled C program closes right after showing output—no source code needed. Here are three practical ways to make the window stay open so you can read your results:

1. Run the EXE from Command Prompt/PowerShell

This is the simplest fix, no extra files required:

  • Open Command Prompt (press Win+R, type cmd, hit Enter) or PowerShell.
  • Navigate to the folder where your EXE is stored using the cd command. For example:
    cd C:\Users\YourName\Desktop\ProgramFiles
    
  • Type the name of your EXE file (like myprogram.exe) and press Enter. The window will stay open after the program finishes, so you can see all output.

2. Create a Batch File to Wrap the EXE

If you prefer double-clicking like before, make a batch file to add a pause after the program runs:

  • Right-click in the same folder as your EXE, select "New > Text Document".
  • Open the text file and paste these two lines (replace your_program.exe with your actual EXE name):
    your_program.exe
    pause
    
  • Save the file, then rename it from New Text Document.txt to run_program.bat (make sure file extensions are visible so you can change .txt to .bat).
  • Double-click the .bat file—your program will run, and after it finishes, the window will display "Press any key to continue . . ." so you can read the output.

3. Modify the EXE with a Debugger (Advanced)

If you want a permanent fix without relying on batch files or command lines, you can patch the EXE to add a getchar() call before exiting. This needs basic reverse-engineering tools like x64dbg:

  • Download and open x64dbg, then load your EXE.
  • Find the end of the main function (look for the ret instruction that exits the program).
  • Before that ret instruction, insert a call to getchar(). You'll need to locate the address of getchar from the program's imported functions.
  • Save the modified EXE (use x64dbg's "Patch file" feature). Now the program will automatically pause before closing.

Note: The first two methods are perfect for quick use, while the third is better if you want a self-contained fix that works with double-clicks.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:49:28