C++中如何移动文件指针到下一行?写入记录覆盖问题求解
Let's fix your issues step by step—you're running into two main problems: overwriting existing records, and not getting your fields aligned to the 0, 24, 56 positions per line. Here's how to resolve both:
1. Stop Overwriting Existing Records
Your current code uses f2.seekp(0, ios::beg) which jumps straight to the start of the file every time you write—this is why old records get overwritten. Instead, you need to either:
- Open the file in append mode when you initialize
f2, so new writes automatically go to the end of the file:fstream f2("your_filename.txt", ios::out | ios::app); - Or, if you need to keep the file open for multiple writes, explicitly seek to the end before each write:
f2.seekp(0, ios::end);
2. Align Fields to Fixed Positions
To get customerno starting at 0, customername at 24, and product at 56, each field needs to occupy a fixed width of characters. If your string is shorter than the required width, you'll need to pad it with spaces to fill the gap.
Option 1: Use C++ Stream Manipulators (Simpler)
Include the <iomanip> header first, then use setw() to define field widths and left to align text to the left (adjust to right if you want right-aligned fields):
#include <iomanip> // Required for setw and left/right void filewrite(fstream &f2) { // Ensure we're writing to the end of the file f2.seekp(0, ios::end); // Set left alignment so padding spaces go to the right of the text f2 << left; // customerno takes 24 characters (positions 0-23) f2 << setw(24) << customerno; // customername takes 32 characters (positions 24-55, since 56-24=32) f2 << setw(32) << customername; // product starts at 56, write it followed by a newline to end the record f2 << product << endl; }
Option 2: Manual Padding with write() (If You Prefer C-Style Strings)
If you want to stick with the write() method, calculate how many spaces you need to add to each field to reach the required width:
#include <string> // For std::string to create padding spaces void filewrite(fstream &f2) { // Move to the end of the file first f2.seekp(0, ios::end); // Write customerno + padding to 24 characters int no_length = strlen(customerno); f2.write(customerno, no_length); if (no_length < 24) { // Create a string of (24 - no_length) spaces and write it f2.write(std::string(24 - no_length, ' ').c_str(), 24 - no_length); } // Write customername + padding to 32 characters (to reach position 56) int name_length = strlen(customername); f2.write(customername, name_length); if (name_length < 32) { f2.write(std::string(32 - name_length, ' ').c_str(), 32 - name_length); } // Write product and end the line f2.write(product, strlen(product)); f2 << '\n'; // Use '\n' instead of endl if you don't need to flush the buffer immediately }
Why Your Original Code Failed
seekp(0, ios::beg)forced every write to start at the file's beginning, overwriting old data.- You didn't pad shorter strings with spaces, so
customernamewould start right after the end ofcustomernoinstead of at position 24.
内容的提问来源于stack exchange,提问作者Vinita Kumari

