C++中,譬如我设定了一个student类,里面有int age,string name,string Num,并且我已经将他们通过链表类串联,并且txt文件写入了student.txt中,那么比如我存了一些数据,我要指定修改某一个对象的age值再放回文件,我该怎么操作
时间: 2023-11-26 16:05:10 浏览: 130
要修改 student.txt 文件中某个学生对象的 age 值,可以先读取该文件中的所有学生对象数据,然后找到需要修改的学生对象,将其 age 值修改后再重新写入到 student.txt 文件中。以下是一个示例代码,可以实现该功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
public:
Student(int age, string name, string num) : age(age), name(name), num(num) {}
int getAge() const {
return age;
}
void setAge(int age) {
this->age = age;
}
string getName() const {
return name;
}
void setName(string name) {
this->name = name;
}
string getNum() const {
return num;
}
void setNum(string num) {
this->num = num;
}
void writeToTxt(ofstream& file) const {
file << age << " " << name << " " << num << endl;
}
private:
int age;
string name;
string num;
};
class LinkedList {
public:
LinkedList() : head(nullptr) {}
void add(Student* student) {
Node* newNode = new Node;
newNode->student = student;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
void display() const {
Node* temp = head;
while (temp != nullptr) {
cout << "Name: " << temp->student->getName() << ", Age: " << temp->student->getAge() << ", Num: " << temp->student->getNum() << endl;
temp = temp->next;
}
}
void writeToTxt(string fileName) const {
ofstream file(fileName);
Node* temp = head;
while (temp != nullptr) {
temp->student->writeToTxt(file);
temp = temp->next;
}
file.close();
}
void modifyAge(string num, int newAge) {
Node* temp = head;
while (temp != nullptr) {
if (temp->student->getNum() == num) {
temp->student->setAge(newAge);
break;
}
temp = temp->next;
}
}
~LinkedList() {
Node* temp = head;
while (temp != nullptr) {
Node* next = temp->next;
delete temp->student;
delete temp;
temp = next;
}
}
private:
struct Node {
Student* student;
Node* next;
};
Node* head;
};
LinkedList* readFromTxt(string fileName) {
LinkedList* list = new LinkedList();
ifstream file(fileName);
if (file.is_open()) {
string line;
while (getline(file, line)) {
int age;
string name, num;
stringstream ss(line);
ss >> age >> name >> num;
Student* student = new Student(age, name, num);
list->add(student);
}
file.close();
} else {
cout << "Unable to open file " << fileName << endl;
}
return list;
}
int main() {
LinkedList* list = readFromTxt("student.txt");
cout << "Before modification:" << endl;
list->display();
list->modifyAge("001", 20);
cout << "After modification:" << endl;
list->display();
list->writeToTxt("student.txt");
delete list;
return 0;
}
```
在以上示例代码中,我们定义了一个 Student 类和一个 LinkedList 类。Student 类中包含了 age、name 和 num 三个属性,并提供了读取和修改这些属性的方法以及将该对象写入 txt 文件的方法。LinkedList 类中包含了添加学生对象、展示学生对象、将学生对象写入 txt 文件和修改学生对象 age 值的方法。在 main 函数中,我们首先调用 readFromTxt 方法读取 student.txt 文件中的所有学生对象数据,并将其存储到 LinkedList 对象中。然后,我们展示了所有学生对象的信息,并调用 modifyAge 方法修改了学号为 "001" 的学生对象的 age 值。最后,我们将修改后的学生对象数据重新写入到 student.txt 文件中。需要注意的是,在读取文件时,需要使用 ifstream 类型的对象,并使用 getline 函数逐行读取文件中的内容。同时,在修改数据时,需要遍历链表找到需要修改的学生对象,并调用其 setAge 方法修改其 age 值。
阅读全文
相关推荐


















