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

Ubuntu下新手用GDB调试C++文件写入状态的方法

Step-by-Step Guide to Debugging Your C++ File Handling Code with GDB on Ubuntu

Hey there! Let's walk through how to use GDB to get to the bottom of why your file writing isn't working. Since you're new to debugging, I'll keep this simple and step-by-step.

First, Compile Your Code with Debug Symbols

Before you can debug with GDB, you need to compile your C++ code with debug information included—this lets GDB map your source code lines to the running program. Open your terminal and run:

g++ -g your_code_file.cpp -o your_program

The -g flag is crucial here; without it, GDB won't be able to show you which line of code is running.

Launch GDB

Start GDB with your compiled program:

gdb ./your_program

You'll see the GDB prompt ((gdb)) where you can enter debugging commands.

Set Breakpoints to Pause Execution

Breakpoints let you stop the program at specific lines so you can inspect what's happening. Let's say your file handling logic starts at line 20 in main.cpp—set a breakpoint there:

break main.cpp:20

Or if you have a dedicated function for writing to the file, break at that function name:

break write_to_file_function

Run the Program

Start executing your program within GDB:

run

The program will run until it hits your breakpoint, then pause so you can dig into details.

Step Through Code and Inspect Variables

Once paused, use these commands to move through your code:

  • next: Execute the next line (skips over function calls, good for moving quickly)
  • step: Execute the next line and step into function calls (perfect for checking your overloaded returnDon() method)

To check variables or the state of your file stream:

  • Print the value of total: print total
  • Check if your file stream is in a valid state: print myfile.good() (returns true if no errors are present)
  • Check if the stream hit the end of the file: print myfile.eof() (this will be true after your while(myfile>>total) loop)

Key Issue in Your Code Snippet

Looking at your code, I spot a common mistake that's likely causing the write failure:

if(myfile) { while(myfile>> total) {} cout << " Total : " << total << endl; cout << " PLz donate : "; cin >> p; //Operator is Overloaded string s=p.returnDon();...

That while(myfile>>total) loop reads from the file until it hits EOF (end of file). Once that happens, the file stream sets error flags (eofbit and failbit) which put it in an invalid state. If you try to write to myfile right after this loop, it won't work—since the stream thinks it's still in an error state. To fix this, add these lines before attempting to write:

myfile.clear(); // Resets all error flags so the stream is usable again
myfile.seekp(0); // Optional: Use this if you want to write at the start of the file (skip if appending)

Verify File Content Mid-Debug

At any point, you can run a shell command directly from GDB to check if the file has been written to. For example, if your file is named donations.txt:

!cat donations.txt

The ! tells GDB to run the command in your terminal.

Exit GDB

When you're done debugging, type quit to exit GDB.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:58:30