Thuta Learning
C++
AdvancedProgrammingအခြေခံ

File I/O

C++သင်ခန်းစာ 2530 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • std::ofstream နဲ့ file ကို write ဒါမှမဟုတ် append mode နဲ့ ဖွင့်ပြီး မသုံးခင် is_open() နဲ့ စစ်နိုင်ဖို့
  • while (std::getline(in, line)) နဲ့ line တစ်ကြောင်းချင်း ဖတ်ပြီး while (!in.eof()) ဘာကြောင့် မှားလဲ ရှင်းပြနိုင်ဖို့
  • Buffering နဲ့ RAII က စာသားကို disk ပေါ် ဘယ်အချိန် ရောက်စေလဲ ရှင်းပြနိုင်ဖို့
  • std::filesystem နဲ့ path တည်ဆောက်ပြီး line တစ်ကြောင်းချင်းကို std::istringstream နဲ့ parse လုပ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

File I/O ကို ရိုးရိုးလေး ပြောရရင်

Program ရဲ့ variable တွေက program ပိတ်လိုက်တာနဲ့ ပျောက်သွားပါတယ်။ Settings၊ high-score ဇယား၊ ညတစ်ညလုံး ဘာတွေ ဖြစ်ခဲ့လဲဆိုတဲ့ log စတဲ့ data တွေကို နောက်တစ်ခါ run တဲ့အထိ ထိန်းထားပေးတာက file ပါ။ C++ က file ကို screen နဲ့ keyboard လိုပဲ character တွေ စီးဆင်းနေတဲ့ stream တစ်ခုအဖြစ် ဆက်ဆံပါတယ်။ std::cout ကို << နဲ့ ရေးဖူးပြီးသားပါ၊ std::ofstream ကလည်း အဲဒီ အတွေးအခေါ်အတိုင်းပါပဲ၊ character တွေက screen အစား file ထဲ ရောက်သွားတာပဲ ကွာပါတယ်။

ဖတ်တာကတော့ ပြောင်းပြန်ပါ။ std::ifstream က file ထဲက character တွေကို ပေးပြီး std::getline က ဈေးဝယ်စာရင်းကို အပေါ်ကနေ အောက်ကို ဖတ်သလို line တစ်ကြောင်းချင်း ဆွဲထုတ်ပေးပါတယ်။ Class နှစ်ခုလုံး <fstream> header ထဲမှာ ရှိပါတယ်။

Problem → Why → Solution

ပြဿနာ: ခုနက ရေးလိုက်တဲ့ စာ ဘယ်ရောက်သွားလဲ

File တစ်ခုထဲ မှတ်စုတစ်ကြောင်း ရေးပြီး function တစ်ခုတည်းထဲမှာပဲ line အနည်းငယ် အကြာမှာ user ကို ပြဖို့ ပြန်ဖွင့်တယ် ဆိုပါစို့။ Program က ဘာမှ print မထုတ်ပါဘူး။ Error လည်း မရှိ၊ crash လည်း မဖြစ်ပါဘူး၊ program ပြီးသွားတော့ file ကို ဖွင့်ကြည့်ရင် စာသား မှန်မှန်ကန်ကန် ရှိနေပါသေးတယ်။ မှားသွားတာက အချိန်ပါ: ဖတ်တဲ့အချိန်မှာ စာသားက ရေးတဲ့ stream ရဲ့ memory ထဲမှာ စောင့်နေတုန်း ဖြစ်နေလို့ပါ။

read_too_early.cpp
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ofstream out("notes.txt");
    out << "remember the milk\n";

    std::ifstream in("notes.txt");       // out is still open: text is in its buffer
    std::string line;
    if (std::getline(in, line)) {
        std::cout << "read: " << line << '\n';
    } else {
        std::cout << "nothing to read yet\n";   // this branch runs
    }
}

ဖတ်တာ ဘာကြောင့် ဗလာ ပြန်လာတာလဲ

std::ofstream က << တစ်ခါရေးတိုင်း disk ဆီ တန်းမပို့ပါဘူး။ Operating system ဆီ တစ်ခါသွားတိုင်း နှေးလို့ character တွေကို buffer ထဲ စုထားပြီး အစုလိုက် ရေးပါတယ်။ Buffer ပြည့်တဲ့အခါ၊ flush() ဒါမှမဟုတ် close() ကို ခေါ်တဲ့အခါ၊ stream object ပျက်သွားတဲ့အခါမှ flush ဖြစ်ပါတယ်။ အဲဒီအချိန်မတိုင်ခင် file တူတူကို ဖွင့်တဲ့ ဒုတိယ stream က disk ပေါ်မှာ ရှိတာကိုပဲ မြင်ရပြီး ဒီမှာတော့ အဲဒါက ဘာမှ မရှိသေးပါဘူး။

Core concept: stream ကို ဖွင့်၊ စစ်၊ လွှဲ၊ ပိတ်

<fstream> မှာ class သုံးခု ရှိပါတယ်: ရေးဖို့ std::ofstream၊ ဖတ်ဖို့ std::ifstream၊ နှစ်မျိုးလုံးအတွက် std::fstream ပါ။ Constructor ကို path ပေးလိုက်တာနဲ့ file ကို ဖွင့်ပါတယ်။ ဖွင့်တာ မအောင်မြင်နိုင်ပါဘူး — folder မရှိတာ၊ permission မရှိတာ၊ နာမည် စာလုံးပေါင်းမှားတာ ဖြစ်နိုင်ပါတယ်။ အရေးကြီးတာက stream က default အနေနဲ့ exception မ throw ပါဘူး၊ fail flag တစ်ခု set လုပ်ပြီး နောက်ထပ် operation တွေကို အသံမထွက်ဘဲ လျစ်လျူရှုပါတယ်။ ဒါကြောင့် ဖွင့်ပြီးတာနဲ့ is_open() ဒါမှမဟုတ် if (!out) နဲ့ ချက်ချင်း စစ်ရပါတယ်။

ဖွင့်ပုံဘာလုပ်လဲ
std::ofstream out(path)File အသစ် ဖန်တီး၊ ရှိပြီးသားဆိုရင် အထဲကဟာတွေ ဖျက်ပစ်
std::ofstream out(path, std::ios::app)ရှိပြီးသားကို ထိန်းထားပြီး ရေးသမျှ နောက်ဆုံးမှာ ထပ်ဆက်
std::ifstream in(path)ဖတ်ဖို့ ဖွင့်၊ file မရှိရင် fail ဖြစ်
... | std::ios::binaryNewline ပြောင်းလဲမှု မရှိ၊ byte တွေ ရေးတဲ့အတိုင်း ကျန်

ဖတ်တဲ့အခါ စံပုံစံက while (std::getline(in, line)) ပါ။ getline က stream ကိုယ်တိုင်ကို ပြန်ပေးပြီး ဖတ်တာ fail ဖြစ်သွားတာနဲ့ stream က false ဖြစ်သွားတဲ့အတွက် loop body က တကယ် ဖတ်ခဲ့ရတဲ့ line တွေအတွက်ပဲ run ပါတယ်။ >> operator ကတော့ whitespace နဲ့ ခွဲထားတဲ့ word ဒါမှမဟုတ် number တစ်ခုကို ဖတ်ပြီး getline က '\n' မတိုင်ခင်ထိ line တစ်ကြောင်းလုံးကို ဖတ်ပါတယ်။

Stream မှာ state flag လေးခု ရှိပါတယ်: good၊ eof၊ fail၊ bad။ eof က ဖတ်တာ တစ်ခုက file အဆုံးကို ကျော်ဖို့ ကြိုးစားပြီးမှ set ဖြစ်တာပါ၊ ဒါကြောင့် အဲဒါက ဖြစ်ပြီးသားကို ပြောတဲ့ report ဖြစ်ပြီး ရှေ့ဖြစ်မယ့်ကို ကြိုပြောတာ မဟုတ်ပါဘူး။

File stream တွေက RAII ကို လိုက်နာပါတယ်: destructor က buffer ကို flush လုပ်ပြီး file ကို ပိတ်ပါတယ်။ ဒါကြောင့် ရေးတဲ့ stream ကို { } scope လေးထဲ ထည့်ထားတာက reader မဖွင့်ခင် data disk ပေါ် ရောက်ပြီးကြောင်း အာမခံဖို့ အရှင်းဆုံး နည်းပါ။ Path တွေကိုတော့ std::filesystem::path နဲ့ တည်ဆောက်ပါ။ "data.txt" လို relative path က .cpp file ရှိတဲ့ နေရာကို မဟုတ်ဘဲ program ကို စတင် run ခဲ့တဲ့ current working directory ကို ကိုးကားတာကို သတိထားပါ။

File stream ကို buffer ပါတဲ့ ပိုက်လို မြင်ကြည့်ပါ
  1. Open: constructor က path နဲ့ ချိတ်
  2. Check: မသုံးခင် is_open() စစ်
  3. Transfer: << / getline က buffer ကနေ
  4. Close: destructor က buffer ကို flush

ရေးလိုက်တဲ့ data က flush၊ close ဒါမှမဟုတ် stream ရဲ့ scope ပြီးသွားမှသာ disk ပေါ် ရောက်ကြောင်း သေချာပါတယ်။

အထွေထွေ ပုံစံ

cpp
std::ofstream out(path);            // or std::ofstream out(path, std::ios::app);
if (!out.is_open()) { /* report and stop */ }
out << value << '\n';
out.close();                        // or simply let the scope end

std::ifstream in(path);
if (!in.is_open()) { /* report and stop */ }
std::string line;
while (std::getline(in, line)) {
    // use line
}

ရေးတာရော ဖတ်တာပါ ဖွင့်၊ စစ်၊ လွှဲ၊ ပိတ် ဆိုတဲ့ အစီအစဉ်အတိုင်းပါပဲ။ File တူတူကို reader မဖွင့်ခင် writer ကို ပိတ်ထား ဒါမှမဟုတ် scope ပြင်ပ ရောက်နေရမယ်၊ read loop ရဲ့ condition ကလည်း ဖတ်တဲ့ operation ကိုယ်တိုင် ဖြစ်ရပါမယ်။

Worked example: score file တစ်ခု ရေး၊ ပြန်ဖတ်၊ ရှင်းလင်း

ဒီ program က system ရဲ့ temporary folder ထဲမှာ နာမည် မထပ်တဲ့ file တစ်ခု ဖန်တီး၊ record သုံးခု ရေး၊ line နံပါတ်တွေနဲ့ ပြန်ဖတ်ပြီး file ကို ဖျက်ပါတယ်။ Writer ပတ်လည်က အပို brace တစ်စုံကို သတိထားကြည့်ပါ: အဲဒါက ofstream ရဲ့ သက်တမ်းကို အဆုံးသတ်ပေးလို့ reader က path တူတူကို မဖွင့်ခင် flush လုပ်ပြီး ပိတ်ပြီးသား ဖြစ်နေပါတယ်။ Temporary folder က စက်တိုင်းမှာ မတူတဲ့အတွက် path အပြည့်အစုံကို ဘယ်တော့မှ print မထုတ်ဘဲ file နာမည်ကိုပဲ ထုတ်ပါတယ်။

score_file.cpp
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

namespace fs = std::filesystem;

int main() {
    auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
    fs::path path = fs::temp_directory_path() /
                    ("scores-" + std::to_string(stamp) + ".txt");

    {
        std::ofstream out(path);                 // creates or truncates the file
        if (!out.is_open()) {
            std::cerr << "cannot create " << path.filename() << '\n';
            return 1;
        }
        out << "Aye 82\n";
        out << "Ko 91\n";
        out << "Su 77\n";
    }                                            // out is flushed and closed here

    std::ifstream in(path);
    if (!in.is_open()) {
        std::cerr << "cannot open " << path.filename() << '\n';
        return 1;
    }

    std::string line;
    int lineNumber = 0;
    while (std::getline(in, line)) {             // stops at end of file
        ++lineNumber;
        std::cout << lineNumber << ": " << line << '\n';
    }
    in.close();

    std::cout << "lines read: " << lineNumber << '\n';
    std::cout << "removed: " << std::boolalpha << fs::remove(path) << '\n';
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. A unique path in the temp folderfs::path path = fs::temp_directory_path() /

    temp_directory_path() က operating system ရဲ့ scratch folder ကို ပြန်ပေးပြီး operator/ က မှန်ကန်တဲ့ separator နဲ့ ဆက်ပေးပါတယ်။ Clock ရဲ့ tick အရေအတွက်ကို နာမည်ထဲ ထည့်ထားလို့ run နှစ်ခု တိုက်မိမှာ မဟုတ်ပါဘူး။

  2. Open and check at onceif (!out.is_open()) {

    Constructor က file ကို ဖွင့်ပြီးသားပါ၊ ဒီ line က အဲဒါ အောင်မြင်ခဲ့လား မေးပါတယ်။ မအောင်မြင်ရင် std::cerr ကို file နာမည်နဲ့ message ရေးပြီး return 1 နဲ့ ထွက်တဲ့အတွက် ပျက်နေတဲ့ stream ကို ဘယ်တော့မှ မသုံးမိပါဘူး။

  3. The scope closes the writerstd::ofstream out(path);

    out ကို { } block ထဲမှာ ကြေညာထားလို့ closing brace ရောက်တာနဲ့ destructor run ပြီး buffer ထဲက line သုံးကြောင်းလုံး disk ပေါ် ရောက်သွားပါတယ်။ Default mode ဖြစ်တဲ့အတွက် ရှိပြီးသား file ဆိုရင် အရင်ဆုံး ဗလာ ဖြစ်သွားပါမယ်။

  4. The read is the loop conditionwhile (std::getline(in, line)) {

    Line တစ်ကြောင်း ဖတ်လို့ ရရင် body run ပါတယ်။ စတုတ္ထမြောက် getline က file အဆုံးကို တွေ့တဲ့အခါ stream က false ဖြစ်ပြီး loop ရပ်သွားတဲ့အတွက် count က 3 အတိအကျ ဖြစ်ပါတယ်။

  5. Clean up after yourselffs::remove(path)

    remove က file တစ်ခုကို တကယ် ဖျက်လိုက်ရင် true ပြန်ပေးလို့ cleanup ကိုလည်း စစ်လို့ ရပါတယ်။ Reader ကို in.close() နဲ့ အရင် ပိတ်ထားတာက Windows မှာ ဖွင့်ထားဆဲ file ကို ဖျက်လို့ မရလို့ပါ။

You should see
1: Aye 82
2: Ko 91
3: Su 77
lines read: 3
removed: true

Record တွေကို parse လုပ်ပြီး line မှားတွေကို ကျော်ခြင်း

လက်တွေ့ file တွေမှာ line တစ်ကြောင်းက field အများကြီး သယ်ထားလေ့ရှိပြီး တစ်ကြောင်းကတော့ ပျက်နေတတ်ပါတယ်။ ယုံကြည်ရတဲ့ ပုံစံက getline နဲ့ line တစ်ကြောင်းလုံး ဖတ်၊ အဲဒီ line ကို std::istringstream ထဲ ထည့်ပြီး >> နဲ့ field တွေ ခွဲထုတ်တာပါ။ Parse မအောင်မြင်ရင် ထိခိုက်တာက အဲဒီ line ရဲ့ string stream လေး တစ်ခုတည်းပါ၊ file stream က ကောင်းနေဆဲမို့ နောက် line ကို ဆက်ဖတ်လို့ ရပါတယ်။

parse_marks.cpp
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

namespace fs = std::filesystem;

int main() {
    auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
    fs::path path = fs::temp_directory_path() /
                    ("marks-" + std::to_string(stamp) + ".txt");
    {
        std::ofstream out(path);
        if (!out.is_open()) return 1;
        out << "Aye 82\nKo ninety\nSu 77\n";
    }

    std::ifstream in(path);
    if (!in.is_open()) return 1;
    std::string line;
    int total = 0, valid = 0, lineNo = 0;
    while (std::getline(in, line)) {
        ++lineNo;
        std::istringstream fields(line);         // parse one line at a time
        std::string name;
        int score = 0;
        if (fields >> name >> score) {
            total += score;
            ++valid;
        } else {
            std::cout << "line " << lineNo << " skipped: " << line << '\n';
        }
    }
    in.close();
    fs::remove(path);
    std::cout << valid << " valid records, total " << total << '\n';
}

ဒီ program က "line 2 skipped: Ko ninety" နဲ့ "2 valid records, total 159" ကို print ထုတ်ပါတယ်။ "ninety" ကို int အဖြစ် ဖတ်လို့ မရတဲ့အတွက် fields >> name >> score က false ဖြစ်ပြီး program က ဆက် run ပါတယ်။ Comma ခြားထားတဲ့ CSV အတွက်ဆိုရင် >> အစား std::getline(fields, cell, ',') ကို သုံးပြီး cell တစ်ခုချင်း ခွဲပါ။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ file I/O

Database သုံးတဲ့ application တွေမှာတောင် ရိုးရိုး text file တွေက နေရာတိုင်းမှာ ရှိနေဆဲပါ။ ဒီ lesson ထဲက ပုံစံတွေက အောက်ပါ အလုပ်တွေကို တိုက်ရိုက် ထောက်ပံ့ပေးပါတယ်:

  • Configuration: startup မှာ key=value line တွေကို ဖတ်ပြီး file မရှိရင် default value တွေ သုံးပါတယ်။ File မရှိတာက error မဟုတ်ဘဲ ပထမဆုံး run ဖြစ်နိုင်လို့ is_open() ရလဒ်ကို သေချာ ကိုင်တွယ်ရပါတယ်။
  • Log file: std::ios::app နဲ့ ဖွင့်လို့ run တိုင်း line အသစ်တွေ ထပ်ဆက်ပါတယ်။ အရေးကြီးတဲ့ event ပြီးတိုင်း flush လုပ်တဲ့ service တွေက crash ဖြစ်ရင်တောင် နောက်ဆုံး message တွေ ပျောက်မသွားပါဘူး။
  • CSV import/export: report တွေကို spreadsheet ဆီ ထုတ်ပေးပြီး bank statement လို data ကို ပြန်ဖတ်ပါတယ်။ Line တစ်ကြောင်း ပျက်နေရုံနဲ့ import တစ်ခုလုံး မရပ်သွားအောင် line တစ်ကြောင်းချင်း parse လုပ်ပါတယ်။
  • Save file: game နဲ့ editor တွေက file အသစ်ကို temporary နာမည်နဲ့ ရေးပြီးမှ std::filesystem::rename နဲ့ အဟောင်းကို အစားထိုးပါတယ်၊ ဒါကြောင့် ရေးနေတုန်း ပါဝါပြတ်ရင်တောင် save အဟောင်း မပျက်ပါဘူး။

အမှားများ

File I/O မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

while (!in.eof()) နဲ့ loop ပတ်ခြင်း

"Aye 82" နဲ့ "Ko 91" ပါတဲ့ file ကို while (!in.eof()) { in >> name >> score; ... } နဲ့ ဖတ်ရင် Ko 91 ကို နှစ်ခါ print ထုတ်ပါတယ်။ ဒုတိယ record ပြီးတဲ့အချိန် eof မ set ရသေးလို့ loop က နောက်တစ်ခါ ပတ်၊ ဖတ်တာ fail ဖြစ်ပေမဲ့ body က value အဟောင်းတွေကို သုံးသွားလို့ပါ။ ဖတ်တဲ့ operation ကို condition ထဲ ထည့်ပါ: while (in >> name >> score)။

File ဖွင့်တာ အောင်မြင်လား မစစ်ခြင်း

ofstream တစ်ခု fail ဖြစ်ရင် နောက်က << တိုင်းက ဘာမှ မလုပ်ဘဲ ကျော်သွားပြီး program က "saved" လို့ ဝမ်းသာအားရ ပြောနေနိုင်ပါတယ်။ ဖွင့်ပြီးတာနဲ့ is_open() ကို စစ်ပြီး ဘယ် file လဲ ပြတဲ့ message ထုတ်ပါ။

>> ပြီးမှ getline ကို ရောသုံးခြင်း

in >> count က number ကို ဖတ်ပေမဲ့ နောက်က '\n' ကို ချန်ထားခဲ့ပါတယ်၊ ဒါကြောင့် နောက် getline က string အလွတ်ကို ချက်ချင်း ပြန်ပေးပါတယ်။ std::getline(in >> std::ws, name) လို whitespace ကို အရင် ကျော်ပါ၊ ဒါမှမဟုတ် line တိုင်းကို getline နဲ့ ဖတ်ပြီး istringstream နဲ့ parse လုပ်ပါ။

Relative path က .cpp file ဘေးကို ညွှန်တယ်လို့ ထင်ခြင်း

"data.txt" ကို current working directory ကနေ ရှာပါတယ်၊ IDE တွေမှာ အဲဒါက build folder ဖြစ်လေ့ရှိပါတယ်။ Terminal ကနေ run ရင် အလုပ်လုပ်ပြီး IDE ကနေ run ရင် fail ဖြစ်နေရင် အဲဒါ အကြောင်းရင်းပါ။

Best Practices

Best practices

  • Stream တစ်ခုချင်းကို လိုအပ်တဲ့ အသေးဆုံး scope ထဲမှာ ထားပါ၊ destructor က flush နဲ့ close ကို သင့်ကိုယ်စား လုပ်ပေးပါတယ်။
  • Error message ထဲမှာ path အပြည့်အစုံ မဟုတ်ဘဲ path.filename() ကိုပဲ ပြပါ၊ user ရဲ့ folder structure ကို log ထဲ မပေါက်ကြားစေဖို့ပါ။
  • "/" ဒါမှမဟုတ် "\\" နဲ့ string တွေ ဆက်မယ့်အစား std::filesystem::path နဲ့ operator/ ကို သုံးပါ။
  • အရေးကြီးတဲ့ file ကို temporary နာမည်နဲ့ ရေးပြီးမှ rename လုပ်ပါ၊ ပြီးရင် test file တွေကို ဖျက်ပါ။

Mini challenge

Mini challenge: append-only deploy log

Log file တစ်ခုအတွက် helper နှစ်ခု ရေးပါ။ appendLog က ရှိပြီးသား စာတွေကို မဖျက်ဘဲ file ရဲ့ နောက်ဆုံးမှာ line တစ်ကြောင်း ထပ်ဆက်ပြီး countLines က file ထဲမှာ line ဘယ်နှကြောင်း ရှိလဲနဲ့ နောက်ဆုံး line ကို ပြန်ပေးပါတယ်။ appendLog ကို သုံးခါ ခေါ်၊ count နဲ့ နောက်ဆုံး line ကို print ထုတ်ပြီး file ကို ဖျက်ကာ countLines က fail ဖြစ်ကြောင်း ပြပါ။

  • appendLog(path, message) က std::ios::app နဲ့ ဖွင့်၊ is_open() စစ်ပြီး fail ဖြစ်ရင် false ပြန်ပေးရမယ်
  • countLines(path, lastLine) က file ဖွင့်မရရင် -1 ပြန်ပေးရမယ်
  • Path ကို std::filesystem::temp_directory_path() နဲ့ နာမည်မထပ်အောင် တည်ဆောက်ပြီး path အပြည့်အစုံကို print မထုတ်ရ
  • နောက်ဆုံးမှာ file ကို ဖျက်ရမယ်
deploy_log.cpp
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

namespace fs = std::filesystem;

bool appendLog(const fs::path& path, const std::string& message) {
    // TODO: open in append mode, check it, write message plus a newline
    return false;
}

int countLines(const fs::path& path, std::string& lastLine) {
    // TODO: return -1 when the file cannot be opened
    // TODO: read with std::getline, count lines, remember the last one
    return 0;
}

int main() {
    // TODO: build a unique path inside fs::temp_directory_path()
    // TODO: append "build started", "tests passed", "deploy finished"
    // TODO: print the line count and the last line
    // TODO: remove the file, then call countLines again
}

ရလဒ်: lines after 3 appends: 3၊ last line: deploy finished နဲ့ after remove: -1 ကို print ထုတ်ပါတယ်။ Run တိုင်း file နာမည်အသစ် သုံးလို့ ထပ် run လည်း 3 ပဲ ရပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

std::ofstream log(path, std::ios::app) က ဘယ်တော့မှ truncate မလုပ်ပါဘူး။ countLines မှာ မရှိတဲ့ file ပေါ်က ifstream က ဖွင့်တာ fail ဖြစ်ရုံပါ၊ ဒါကြောင့် is_open() က သင့်ရဲ့ -1 စစ်ချက်ပါ။ Loop ထဲမှာ lastLine ကို ပတ်တိုင်း overwrite လုပ်ပါ။

အဖြေ ကြည့်ရန်
deploy_log.cpp
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

namespace fs = std::filesystem;

bool appendLog(const fs::path& path, const std::string& message) {
    std::ofstream log(path, std::ios::app);      // append, never truncate
    if (!log.is_open()) return false;
    log << message << '\n';
    return static_cast<bool>(log);               // false if the write failed
}

int countLines(const fs::path& path, std::string& lastLine) {
    std::ifstream in(path);
    if (!in.is_open()) return -1;                // -1 means "could not open"
    int count = 0;
    std::string line;
    while (std::getline(in, line)) {
        ++count;
        lastLine = line;
    }
    return count;
}

int main() {
    auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
    fs::path path = fs::temp_directory_path() /
                    ("deploy-" + std::to_string(stamp) + ".log");

    for (const char* step : {"build started", "tests passed", "deploy finished"}) {
        if (!appendLog(path, step)) {
            std::cerr << "cannot write " << path.filename() << '\n';
            return 1;
        }
    }

    std::string last;
    std::cout << "lines after 3 appends: " << countLines(path, last) << '\n';
    std::cout << "last line: " << last << '\n';

    fs::remove(path);
    std::cout << "after remove: " << countLines(path, last) << '\n';
}

Quick Quiz 1 / 3

File ထဲမှာ "Aye 82" နဲ့ "Ko 91" line နှစ်ကြောင်း ရှိပါတယ်။ while (!in.eof()) { in >> name >> score; std::cout << name << ' ' << score << '\n'; } က ဘာ print ထုတ်မလဲ?

Quick Quiz 2 / 3

Problem section ထဲက program မှာ reader က စာကို မြင်ရအောင် ဘာပြောင်းရမလဲ?

Quick Quiz 3 / 3

Run တိုင်း history.txt ထဲ line အသစ်တွေ ထပ်ဆက်ချင်ပြီး run အဟောင်းတွေရဲ့ line တွေ မပျောက်စေချင်ပါဘူး။ ဘယ် line ကို သုံးရမလဲ?

မှတ်ထားရမယ့် အချက်များ

  • std::ofstream က ရေး၊ std::ifstream က ဖတ်ပြီး ဖွင့်ပြီးတိုင်း is_open() နဲ့ စစ်ရပါတယ်၊ stream တွေက default အနေနဲ့ exception မ throw ပါဘူး။
  • Default ofstream က file ကို ဗလာဖြစ်စေပါတယ်၊ ထပ်ဆက်ချင်ရင် std::ios::app ကို သုံးပါ။
  • while (std::getline(in, line)) က မှန်ကန်တဲ့ read loop ပါ၊ eof() က ဖြစ်ပြီးသားကိုပဲ report လုပ်ပါတယ်။
  • ရေးထားတာက flush၊ close ဒါမှမဟုတ် scope ပြီးမှ disk ပေါ် သေချာ ရောက်ပါတယ်၊ ဒါကြောင့် writer ကို { } ထဲ ထည့်ပါ။
  • Path တွေကို std::filesystem နဲ့ တည်ဆောက်ပြီး relative path က working directory ကို ကိုးကားတာ သတိရပါ။

The Current ISO C++ Standard (C++23) — Standard C++ Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

File I/O | Thuta Learning