นี่คือโปรแกรม C++ สำหรับเพิ่มข้อความ (append) ลงในไฟล์ข้อความ
อัลกอริทึม
เริ่มต้น
เปิดไฟล์ a1.txt เป็น output file stream เพื่อทำงานในโหมด append ด้วย fout
ถ้าไฟล์มีอยู่ แล้ว
เพิ่มข้อความลงในไฟล์นั้น
ปิด fout
เปิดไฟล์ "a1.txt" เพื่ออ่านเนื้อหา
ดึงข้อความจากไฟล์และแสดงผล
สิ้นสุด
ตัวอย่างโค้ด
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
fstream f;
ofstream fout;
ifstream fin;
fin.open("a1.txt");
fout.open("a1.txt", ios::app); // เปิดในโหมด append
if (fin.is_open()) {
fout << " tutorials point";
cout << "\n Data has been appended to file" << endl;
fin.close();
fout.close();
string word;
f.open("a1.txt");
while (f >> word) {
cout << word << " ";
}
return 0;
}
}
ผลลัพธ์
Data has been appended to file