C++读取文件到Cust对象异常及多行读取方法咨询
Hey there! Let's work through the problems you're facing with reading file data into your Cust objects, and then cover how to handle multi-line content too.
First: Fixing the Single-Line Read & File Emptying Issues
Your output anomalies (missing name, wrong boolean mapping, garbage numbers) and unexpected file emptying are likely from a few common mistakes—let's break them down and fix them:
1. Stop Accidentally Emptying Your File
If your code was using ofstream instead of ifstream to open the file, or added flags like ios::trunc (which truncates/clears the file), that's why your file content vanished. Always use ifstream for reading—it defaults to ios::in mode, which only reads the file without modifying it.
2. Handle "true"/"false" Strings for Boolean Values
C++'s ifstream doesn't natively read string values like "true" into a bool variable (it expects 0/1 instead). Trying to do this breaks the read sequence, leading to garbage values for subsequent variables. Instead:
- Read the boolean value as a
stringfirst - Convert it to a
boolmanually by checking if the string equals "true"
3. Correct Read Sequence & Variable Types
Make sure your read order exactly matches the file's field order: name → robber string → time → items, and that each variable's type matches what you're reading.
Working Single-Line Read Code Example
#include <fstream> #include <string> #include <iostream> using namespace std; class Cust { private: string m_name; bool m_robber; int m_time; int m_items; public: Cust(string name, bool robber, int time, int items) : m_name(name), m_robber(robber), m_time(time), m_items(items) {} void print() { cout << (m_robber ? "robber" : "shopper") << " " << m_name << " " << m_time << " " << m_items << endl; } }; int main() { // Use ifstream for reading—no extra flags needed ifstream inFile("your_file.txt"); if (!inFile.is_open()) { cerr << "Error: Couldn't open the file!" << endl; return 1; } string name; string robberStr; // Capture "true"/"false" as string first int time, items; // Read the four fields in order inFile >> name >> robberStr >> time >> items; // Convert string to bool bool isRobber = (robberStr == "true"); Cust cust(name, isRobber, time, items); cust.print(); inFile.close(); return 0; }
Reading Multiple Lines of Data
To process multiple lines (like your example: Robbie true 1 2 and Shaun false 3 4), you can use a loop that runs until the stream reaches the end of the file. The while loop will automatically exit when inFile can't read any more data (e.g., end of file).
Multi-Line Read Code Example
int main() { ifstream inFile("your_file.txt"); if (!inFile.is_open()) { cerr << "Error: Couldn't open the file!" << endl; return 1; } string name; string robberStr; int time, items; // Loop until no more data can be read while (inFile >> name >> robberStr >> time >> items) { bool isRobber = (robberStr == "true"); Cust cust(name, isRobber, time, items); cust.print(); } inFile.close(); return 0; }
Quick Final Tips
- Always check if the file opened successfully with
!inFile.is_open()—this saves you from debugging silent failures - If your file has lines with whitespace in the name (e.g., "Robbie the Robber"), you'll need to adjust reading logic (use
getlinewith a delimiter), but your current format (single-word names) works fine with>> - Avoid using
using namespace std;in large projects, but it's okay for small test code like this
内容的提问来源于stack exchange,提问作者Zevvysan

