Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

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;
};

const char FILENAME[] = "hocsinh.txt";


const int MAX_STUDENTS = 100;

void hienthithongtin(const student students[], int numstudents) {


cout << "Danh sach hoc sinh:\n";
cout << "-----------------------------------------------\n";
cout << "Ten hoc sinh\t|\tTuoi\t|\tDiem\n";
cout << "-----------------------------------------------\n";

for (int i=0;i< numstudents; i++) {


cout << students[i].name << "\t|\t" << students[i].age << "\t|\t" <<
students[i].point << "\n";
}

cout << "-----------------------------------------------\n";


}

void themhocsinh(student students[], int& numstudents) {


if (numstudents < MAX_STUDENTS) {
cout << "Nhap thong tin hoc sinh moi:\n";

cout << "Ten hoc sinh: ";


cin.ignore();
cin.getline(students[numstudents].name,
sizeof(students[numstudents].name));

cout << "Tuoi: ";


cin >> students[numstudents].age;

cout << "Diem: ";


cin >> students[numstudents].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;
};

const char FILENAME[] = "sanpham.txt";


const int MAX_PRODUCTS = 100;

void sanphamtrungbay(const product products[], int numproducts) {


cout << "Danh sach san pham:\n";
cout << "-----------------------------------------------\n";
cout << "Ten san pham\t|\tGia\t|\tSoluong\n";
cout << "-----------------------------------------------\n";

for (int i=0;i< numproducts; i++) {


cout << products[i].name << "\t|\t" << products[i].price << "\t|\t" <<
products[i].quantity << "\n";
}

cout << "-----------------------------------------------\n";


}

void themsanpham(product products[], int& numproducts) {


if (numproducts < MAX_PRODUCTS) {
cout << "Nhap thong tin san pham moi:\n";

cout << "Ten san pham: ";


cin.ignore();
cin.getline(products[numproducts].name,
sizeof(products[numproducts].name));

cout << "Gia: ";


cin >> products[numproducts].price;

cout << "So luong: ";


cin >> products[numproducts].quantity;

numproducts++;
cout << "San pham da duoc them vao.\n";
} else {
cout << "Danh sach san pham da day, khong the them vao.\n";
}
}

void luuvaofile(const product products[], int numproducts) {


ofstream file(FILENAME);

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;
}

Bài 3: DEV XANH EM CHẠY KHÔNG ĐƯỢC NÊN DÙNG ONLINE Ạ.


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct Event {
string ngay;
string gio;
string diadiem;
string mota;
};

void hienthisukien(const vector<Event>& events) {


cout << "Danh sach su kien:" << endl;
for (const Event& event : events) {
cout << "Ngay: " << event.ngay << ", Thoi gian: " << event.gio
<< ", Dia diem: " << event.diadiem << ", Mo ta: " << event.mota << endl;
}
}

void themsukien(vector<Event>& events, const Event& newevent) {


events.push_back(newevent);
}

void luuvaofile(const vector<Event>& events, const string& filename) {


ofstream file(filename);
if (file.is_open()) {
for (const Event& event : events) {
file << event.ngay << " " << event.gio << " " << event.diadiem << " " <<
event.mota << endl;
}
file.close();
cout << "Du lieu da duoc luu vao " << filename << endl;
} else {
cerr << "Khong the mo tep " << filename << " de ghi du lieu." << endl;
}
}

int main() {
vector<Event> events;

ifstream file("events.txt");
if (file.is_open()) {
Event event;
while (file >> event.ngay >> event.gio >> event.diadiem) {

getline(file >> ws, event.mota);


events.push_back(event);
}
file.close();
}
int choice;
do {
cout << "1. Hien thi thong tin su kien" << endl;
cout << "2. Them su kien" << endl;
cout << "3. Luu danh sach vao tep" << endl;
cout << "0. Thoat" << endl;
cout << "Chon tuy chon(0-3): ";
cin >> choice;

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;
};

void themlienhe(vector<Contact>& contacts) {


Contact newcontact;

cout << "Nhap ten: ";


getline(cin, newcontact.ten);

cout << "Nhap dia chi email: ";


getline(cin, newcontact.diachiemail);

cout << "Nhap so dien thoai: ";


getline(cin, newcontact.sdt);
contacts.push_back(newcontact);

void hienthilienhe(const vector<Contact>& contacts) {


for (const Contact& contact : contacts) {
cout << "Ten: " << contact.ten << endl;
cout << "Dia chi email : " << contact.diachiemail << endl;
cout << "So dien thoai: " << contact.sdt << endl;
}
}

void luuvaofile(const vector<Contact>& contacts, const string& filename) {


ofstream file(filename);

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>

using namespace std;

struct Task {
string tieude;
string mota;
string hethan;
};

void themcongviec(vector<Task>& tasks) {


Task newtask;

cout << "Nhap tieu de cong viec: ";


getline(cin, newtask.tieude);

cout << "Nhap mo ta cong viec: ";


getline(cin, newtask.mota);

cout << "Nhap ngay het han: ";


getline(cin, newtask.hethan);

tasks.push_back(newtask);
}

void hienthicongviec(const vector<Task>& tasks) {


for (const Task& task : tasks) {
cout << "Tieu de: " << task.tieude << endl;
cout << "Mo ta: " << task.mota << endl;
cout << "Ngay het han: " << task.hethan << endl;
cout << "------------------------\n";
}
}

void luuvaofile(const vector<Task>& tasks, const string& filename) {


ofstream file(filename);

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;
}

You might also like