通讯录管理系统c++代码有文件读入
时间: 2023-12-21 08:01:52 浏览: 58
通讯录管理系统是一种能够帮助用户管理联系人信息的程序。它能够实现添加、删除、查找、修改联系人等功能,并且能够将联系人信息保存在文件中,实现数据持久化。
对于通讯录管理系统的代码实现,文件读入是其中一个非常重要的部分。在代码中,可以通过文件读入的方式将之前保存的联系人信息加载到系统中,这样用户就能够在程序中方便地对之前存储的联系人信息进行操作了。
文件读入的代码逻辑通常包括打开文件、读取文件内容、处理读取的信息等步骤。在打开文件时,需要指定文件的路径和打开模式,如只读模式或读写模式。在读取文件内容时,可以使用读取一行或者一定长度的数据的方式,将文件中的内容逐行或逐块地读取出来。读取到的信息可以进一步进行处理,如解析成联系人的姓名、电话号码等信息,然后添加到程序的联系人列表中。
除了文件读入外,通讯录管理系统的代码还需要实现联系人信息的保存、搜索、修改、删除等功能。这些功能也需要和文件读入功能进行配合,以实现文件中联系人信息和程序中联系人信息的同步更新。
综上所述,文件读入是通讯录管理系统代码中的重要部分,它能够实现联系人信息的持久化存储,并且能够让用户方便地管理之前保存的联系人信息。
相关问题
通讯录管理系统c++链表
通讯录管理系统使用C++链表实现,是一种常见的数据结构应用。链表是一种线性数据结构,每个节点(或元素)包含数据和指向下一个节点的指针。在通讯录中,链表可以用来存储联系人信息,如姓名、电话、电子邮件等。
以下是使用C++链表实现通讯录管理系统的基本步骤:
1. **定义节点**:创建一个ContactNode类,包含联系人信息(如std::string name, std::string phone, std::string email)以及指向下一个节点的指针。
```cpp
class ContactNode {
public:
std::string name;
std::string phone;
std::string email;
ContactNode* next;
};
```
2. **创建链表**:定义一个ContactList类,包含链表头指针(通常是nullptr)和链表管理的方法,如添加联系人、删除联系人和搜索联系人。
```cpp
class ContactList {
private:
ContactNode* head;
public:
void addContact(ContactNode* node);
void removeContact(const std::string& name);
ContactNode* searchContact(const std::string& name);
};
```
3. **链表操作**:
- **添加联系人**:从链表头部开始遍历,找到合适的位置插入新节点。
- **删除联系人**:根据名字查找节点,然后调整前一个节点的next指针以跳过要删除的节点。
- **搜索联系人**:从头节点开始逐个比较,直到找到匹配的名字或遍历到链表末尾。
4. **链表遍历**:为了显示所有联系人,你可以实现一个方法来遍历整个链表并打印联系人信息。
```cpp
void ContactList::displayContacts() const {
if (head == nullptr) {
std::cout << "No contacts in the list.\n";
} else {
displayNode(head);
}
}
// 递归函数,遍历单个节点
void displayNode(const ContactNode* node) {
if (node != nullptr) {
std::cout << "Name: " << node->name << ", Phone: " << node->phone << ", Email: " << node->email << "\n";
displayNode(node->next);
}
}
```
通讯录管理系统c++500行左右有注释
以下是一个简单的通讯录管理系统,使用 C++ 编写,大约500行左右,带有注释,可以作为参考:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义联系人结构体
struct Contact {
string name; // 姓名
string phone; // 电话号码
string email; // 邮箱地址
};
// 声明函数
void show_menu();
void add_contact(vector<Contact> &contacts);
void search_contact(const vector<Contact> &contacts);
void delete_contact(vector<Contact> &contacts);
void modify_contact(vector<Contact> &contacts);
void list_all_contacts(const vector<Contact> &contacts);
void save_contacts(const vector<Contact> &contacts);
void load_contacts(vector<Contact> &contacts);
int main() {
vector<Contact> contacts; // 存储所有联系人的容器
load_contacts(contacts); // 从文件加载联系人数据
while (true) { // 无限循环,直到用户选择退出程序
show_menu(); // 显示菜单
char choice;
cin >> choice;
switch (choice) {
case '1':
add_contact(contacts);
break;
case '2':
search_contact(contacts);
break;
case '3':
delete_contact(contacts);
break;
case '4':
modify_contact(contacts);
break;
case '5':
list_all_contacts(contacts);
break;
case '6':
save_contacts(contacts);
break;
case '7':
cout << "Goodbye!" << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
// 显示菜单
void show_menu() {
cout << endl;
cout << "==========================" << endl;
cout << " Contact Management " << endl;
cout << "==========================" << endl;
cout << "1. Add Contact" << endl;
cout << "2. Search Contact" << endl;
cout << "3. Delete Contact" << endl;
cout << "4. Modify Contact" << endl;
cout << "5. List All Contacts" << endl;
cout << "6. Save Contacts" << endl;
cout << "7. Exit" << endl;
cout << endl;
cout << "Enter your choice: ";
}
// 添加联系人
void add_contact(vector<Contact> &contacts) {
Contact contact;
cout << "Enter name: ";
cin >> contact.name;
cout << "Enter phone: ";
cin >> contact.phone;
cout << "Enter email: ";
cin >> contact.email;
contacts.push_back(contact);
cout << "Contact added successfully." << endl;
}
// 搜索联系人
void search_contact(const vector<Contact> &contacts) {
string keyword;
bool found = false;
cout << "Enter keyword: ";
cin >> keyword;
for (const auto &contact : contacts) {
if (contact.name.find(keyword) != string::npos ||
contact.phone.find(keyword) != string::npos ||
contact.email.find(keyword) != string::npos) {
cout << "Name: " << contact.name << endl;
cout << "Phone: " << contact.phone << endl;
cout << "Email: " << contact.email << endl;
cout << endl;
found = true;
}
}
if (!found) {
cout << "No matching contacts found." << endl;
}
}
// 删除联系人
void delete_contact(vector<Contact> &contacts) {
string name;
cout << "Enter name: ";
cin >> name;
auto iter = find_if(contacts.begin(), contacts.end(),
[name](const Contact &contact) { return contact.name == name; });
if (iter != contacts.end()) {
contacts.erase(iter);
cout << "Contact deleted successfully." << endl;
} else {
cout << "Contact not found." << endl;
}
}
// 修改联系人
void modify_contact(vector<Contact> &contacts) {
string name;
cout << "Enter name: ";
cin >> name;
auto iter = find_if(contacts.begin(), contacts.end(),
[name](const Contact &contact) { return contact.name == name; });
if (iter != contacts.end()) {
Contact &contact = *iter;
cout << "Enter new phone: ";
cin >> contact.phone;
cout << "Enter new email: ";
cin >> contact.email;
cout << "Contact modified successfully." << endl;
} else {
cout << "Contact not found." << endl;
}
}
// 列出所有联系人
void list_all_contacts(const vector<Contact> &contacts) {
for (const auto &contact : contacts) {
cout << "Name: " << contact.name << endl;
cout << "Phone: " << contact.phone << endl;
cout << "Email: " << contact.email << endl;
cout << endl;
}
}
// 保存联系人数据到文件
void save_contacts(const vector<Contact> &contacts) {
ofstream outfile("contacts.txt");
if (!outfile.is_open()) {
cout << "Failed to open file." << endl;
return;
}
for (const auto &contact : contacts) {
outfile << contact.name << "," << contact.phone << "," << contact.email << endl;
}
outfile.close();
cout << "Contacts saved successfully." << endl;
}
// 从文件加载联系人数据
void load_contacts(vector<Contact> &contacts) {
ifstream infile("contacts.txt");
if (!infile.is_open()) {
cout << "Failed to open file." << endl;
return;
}
string line;
while (getline(infile, line)) {
Contact contact;
auto pos = line.find(",");
if (pos == string::npos) {
continue;
}
contact.name = line.substr(0, pos);
auto pos2 = line.find(",", pos + 1);
if (pos2 == string::npos) {
continue;
}
contact.phone = line.substr(pos + 1, pos2 - pos - 1);
contact.email = line.substr(pos2 + 1);
contacts.push_back(contact);
}
infile.close();
cout << "Contacts loaded successfully." << endl;
}
```
该程序可以实现以下基本功能:
1. 添加联系人
2. 搜索联系人
3. 删除联系人
4. 修改联系人
5. 列出所有联系人
6. 保存联系人数据到文件
7. 从文件加载联系人数据
其中,联系人数据存储在一个 `vector<Contact>` 容器中,每个联系人记录由一个 `Contact` 结构体表示。程序启动时会从文件加载联系人数据,退出时会将联系人数据保存到文件中。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)