Professional Documents
Culture Documents
X Lý Lưu TR File
X Lý Lưu TR File
Bài 1:
#include <iostream>
#include <fstream>
using namespace std;
struct student {
char name[50];
int age;
float point;
};
numstudents++;
cout << "Hoc sinh da duoc them vao.\n";
} else {
cout << "Danh sach hoc sinh da day, khong the them moi.\n";
}
}
void luuvaofile(const student students[], int numstudents) {
ofstream file(FILENAME);
if (file.is_open()) {
for(int i=0;i < numstudents;i++) {
file << students[i].name << " " << students[i].age << " " <<
students[i].point << " \n";
}
cout << " Du lieu da duoc luu vao tep " << FILENAME << "\n";
file.close();
}else {
cerr << "Khong the mo tep " << FILENAME << " de ghi du lieu.\n";
}
}
int main() {
student students[MAX_STUDENTS];
int n=0;
ifstream file(FILENAME);
if (file.is_open()) {
while (!file.eof() && n< MAX_STUDENTS) {
file >> students[n].name >> students[n].age >> students[n].point;
n++;
}
file.close();
}
int choice;
do {
cout << "Chuong trinh quan li hoc sinh\n";
cout << "1. Hien thi thong tin hoc sinh\n";
cout << "2. Them hoc sinh\n";
cout << "3. Luu du lieu vao tep\n";
cout << "0. Thoat\n";
cout << "Chon chuc nang (0-3): ";
cin >> choice;
switch (choice) {
case 1:
hienthithongtin(students, n);
break;
case 2:
themhocsinh(students, n);
break;
case 3:
luuvaofile(students, n);
break;
case 0:
cout << "Thoat chuong trinh.\n";
break;
default:
cout << "Chuc nang khong hop le, vui long chon lai.\n";
}
}while (choice != 0);
return ;
}
Bài 2:
#include <iostream>
#include <fstream>
using namespace std;
struct product {
char name[50];
float price;
int quantity;
};
numproducts++;
cout << "San pham da duoc them vao.\n";
} else {
cout << "Danh sach san pham da day, khong the them vao.\n";
}
}
if (file.is_open()) {
for(int i=0;i < numproducts;i++) {
file << products[i].name << " " << products[i].price << " " <<
products[i].quantity << " \n";
}
cout << " Du lieu da duoc luu vao tep " << FILENAME << "\n";
file.close();
}else {
cerr << "Khong the mo tep " << FILENAME << " de ghi du lieu.\n";
}
}
int main() {
product products[MAX_PRODUCTS];
int n=0;
ifstream file(FILENAME);
if (file.is_open()) {
while (!file.eof() && n< MAX_PRODUCTS) {
file >> products[n].name >> products[n].price >>
products[n].quantity;
n++;
}
file.close();
}
int choice;
do {
cout << "Chuong trinh quan li san pham\n";
cout << "1. Hien thi thong tin san pham\n";
cout << "2. Them san pham\n";
cout << "3. Luu du lieu vao tep\n";
cout << "0. Thoat\n";
cout << "Chon chuc nang (0-3): ";
cin >> choice;
switch (choice) {
case 1:
sanphamtrungbay(products, n);
break;
case 2:
themsanpham(products, n);
break;
case 3:
luuvaofile(products, n);
break;
case 0:
cout << "Thoat chuong trinh.\n";
break;
default:
cout << "Chuc nang khong hop le, vui long chon lai.\n";
}
} while (choice!= 0);
return 0;
}
struct Event {
string ngay;
string gio;
string diadiem;
string mota;
};
int main() {
vector<Event> events;
ifstream file("events.txt");
if (file.is_open()) {
Event event;
while (file >> event.ngay >> event.gio >> event.diadiem) {
switch (choice) {
case 1:
hienthisukien(events);
break;
case 2: {
Event newevent;
cout << "Nhap ngay su kien (dd/mm/yyyy): ";
cin >> newevent.ngay;
cout << "Nhập thoi gian (hh:mm): ";
cin >> newevent.gio;
cout << "Nhap dia diem: ";
cin.ignore();
getline(cin, newevent.diadiem);
cout << "Nhap mo ta: ";
getline(cin, newevent.mota);
themsukien(events, newevent);
break;
}
case 3:
luuvaofile(events, "events.txt");
break;
case 0:
cout << "Tam biet!" << endl;
break;
default:
cout << "Tuy chon khong hop le. Vui long chon lai." << endl;
}
} while (choice != 0);
return 0;
}
Bài 4:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
struct Contact {
string ten;
string diachiemail;
string sdt;
};
if (file.is_open()) {
for (const Contact& contact : contacts) {
file << contact.ten << "\n";
file << contact.diachiemail << "\n";
file << contact.sdt << "\n";
file << "----------------------\n";
}
cout << "Du lieu da duoc luu vao tep " << filename << endl;
file.close();
} else {
cout << "Khong the mo tep " << filename << "de luu tru du lieu." << endl;
}
}
int main() {
vector<Contact> contacts;
string filename = "danhba.txt";
int choice;
do{
cout << "---Quan ly danh ba---\n";
cout << "1. Them lien he.\n";
cout << "2. Hien thi danh ba.\n";
cout << "3. Luu danh ba vao tep.\n";
cout << "0. Thoat.\n";
cout << "Nhap lua chon(0-3): ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
themlienhe(contacts);
break;
case 2:
hienthilienhe(contacts);
break;
case 3:
luuvaofile(contacts, filename);
break;
case 0:
cout << "Tam biet.\n";
break;
default:
cout << "Lua chon khong hop le. Hay thu lai.\n";
}
} while (choice != 0);
return 0;
}
Bài 5:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
struct Task {
string tieude;
string mota;
string hethan;
};
tasks.push_back(newtask);
}
if (file.is_open()) {
for (const Task& task : tasks) {
file << task.tieude << "\n";
file << task.mota << "\n";
file << task.hethan << "\n";
file << "------------------------\n";
}
cout << "Du lieu da duoc luu vao tep " << filename << endl;
file.close();
} else {
cout << "Khong the mo tep " << filename << " de luu tru du lieu." << endl;
}
}
int main() {
vector<Task> tasks;
string filename = "congviec.txt";
int choice;
do {
cout << "Menu:\n";
cout << "1. Them cong viec\n";
cout << "2. Hien thi danh sach cong viec\n";
cout << "3. Luu danh sach cong viec vao tep\n";
cout << "0. Thoat\n";
cout << "Nhap lua chon: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
themcongviec(tasks);
break;
case 2:
hienthicongviec(tasks);
break;
case 3:
luuvaofile(tasks, filename);
break;
case 0:
cout << "Tam biet!\n";
break;
default:
cout << "Lua chon khong hop le. Hay thu lai.\n";
}
} while (choice != 0);
return 0;
}