C++程序仅读取文件前7行,排查代码异常问题求助
问题排查:C++读取员工数据文件仅显示部分条目
我写了一段C++代码,定义了Employee、Node、LinkedList类,通过readFile函数读取员工数据文件,将数据存入链表并排序后展示。但在多个编译器中,程序仅输出文件中的7行数据,而文件实际包含更多行。逻辑和语法上看似没问题,一开始怀疑是编译器问题,现在觉得代码可能存在错误,尤其是readFile函数或者链表插入逻辑,请求帮忙排查原因。
代码实现
#include <iostream> #include <string> #include <sstream> #include <fstream> using namespace std; class Employee { // employee class public: int employeeId; string firstName; string lastName; string phoneNumber; string emailAddress; double salary; Employee(int id, string first, string last, string phone, string email, double sal) // employee class constructor :employeeId(id), firstName(first), lastName(last), phoneNumber(phone), emailAddress(email), salary(sal) {} void display() { // used to display an object of the employee class cout << "Id: " << employeeId << ", Name: " << firstName << " " << lastName << ", Phone Number: " << phoneNumber << ", Email: " << emailAddress << ", Salary: " << salary << endl; } }; class Node { // class for each node of the linked list public: Employee* info; Node* next; Node(Employee* emp) : info(emp), next(nullptr) {} }; class LinkedList { //the link list class itself private: Node* head; public: LinkedList() : head(nullptr) {} // constructopr that initializes the list with no contents (empty) void insert(Employee* emp) { Node* newNode = new Node(emp); if (head == nullptr || head->info->employeeId >= newNode->info->employeeId) { newNode->next = head; head = newNode; } else { Node* current = head; while (current->next != nullptr && current->next->info->employeeId < newNode->info->employeeId) { current = current->next; } newNode->next = current->next; current = current->next; } } void display() { // function that displays the nodes of the link list Node* current = head; while (current != nullptr) { current->info-> display(); current = current->next; } } ~LinkedList() { Node* current = head; while (current != nullptr) { Node* next = current->next; delete current; current = next; } } }; // Now that all three classes are created and the functions needed with them, // we can now read from the file and add the employees to the linked list void readFile(const string& filename, LinkedList& list) { ifstream file(filename); if (!file.is_open()) { cerr << "Error opening file: " << filename << endl; return; } string line; while (getline(file, line)) { stringstream ss(line); int id; string firstName, lastName, phoneNumber, emailAddress; double salary; ss >> id >> firstName >> lastName >> phoneNumber >> emailAddress >> salary; Employee* emp = new Employee(id, firstName, lastName, phoneNumber, emailAddress, salary); list.insert(emp); } file.close(); } int main() { LinkedList list; readFile("Week 1-employeeDataset_Project 1.txt", list); list.display(); return 0; }
文件示例数据
文件中一行示例数据如下:
12690 Glynis Filipson 539-917-2209 gfilipson56@liveinternet.ru 105401.55
问题根源及修复方案
1. 链表插入函数的致命错误
LinkedList::insert函数的else分支存在逻辑错误:找到合适的插入位置后,代码执行了current = current->next;,这一步完全没有完成插入操作,只是移动了指针,导致新节点根本没有被链接到链表中。只有当节点被插入到链表头部时(if分支)才会成功,其他节点全部丢失,这就是为什么只显示少数行的原因。
修正后的insert函数代码:
void insert(Employee* emp) { Node* newNode = new Node(emp); if (head == nullptr || head->info->employeeId >= newNode->info->employeeId) { newNode->next = head; head = newNode; } else { Node* current = head; while (current->next != nullptr && current->next->info->employeeId < newNode->info->employeeId) { current = current->next; } newNode->next = current->next; current->next = newNode; // 正确的插入操作,将新节点链接到链表中 } }
2. 文件读取的错误处理缺失
当前readFile函数没有检查数据读取是否成功,如果某一行数据格式不符合预期(比如薪资不是数字、字段缺失),ss >> ...会失败,但代码仍会创建无效的Employee对象,甚至可能导致后续的getline因为流状态异常而停止读取。
添加读取校验的修正代码:
void readFile(const string& filename, LinkedList& list) { ifstream file(filename); if (!file.is_open()) { cerr << "Error opening file: " << filename << endl; return; } string line; while (getline(file, line)) { stringstream ss(line); int id; string firstName, lastName, phoneNumber, emailAddress; double salary; // 检查所有字段是否读取成功 if (ss >> id >> firstName >> lastName >> phoneNumber >> emailAddress >> salary) { Employee* emp = new Employee(id, firstName, lastName, phoneNumber, emailAddress, salary); list.insert(emp); } else { cerr << "无效的行格式,跳过该行: " << line << endl; } } file.close(); }
3. 内存泄漏问题
原LinkedList的析构函数只删除了Node对象,但没有删除Node中指向的Employee对象,会导致内存泄漏。需要在析构时同时释放Employee的内存。
修正后的析构函数:
~LinkedList() { Node* current = head; while (current != nullptr) { Node* next = current->next; delete current->info; // 释放Employee对象 delete current; current = next; } }
内容的提问来源于stack exchange,提问作者Kevin Lando
相关产品推荐
相关产品推荐

