Ubuntu下新手用GDB调试C++文件写入状态的方法
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 overloadedreturnDon()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()(returnstrueif no errors are present) - Check if the stream hit the end of the file:
print myfile.eof()(this will betrueafter yourwhile(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

