Thuta Learning
C
AdvancedProgrammingအခြေခံ

File I/O

Cသင်ခန်းစာ 2430 မိနစ်

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

  • fopen() က NULL ပြန်နိုင်တဲ့ အကြောင်းရင်းတွေကို သိပြီး perror() နဲ့ အကြောင်းရင်းကို ပြနိုင်ဖို့
  • "r"၊ "w"၊ "a" နဲ့ "+"၊ "b" ပေါင်းထားတဲ့ mode တွေကို အလုပ်နဲ့ ကိုက်အောင် ရွေးနိုင်ဖို့
  • fgets() loop နဲ့ file ကို line လိုက်ဖတ်ပြီး EOF နဲ့ read error ကို feof()/ferror() နဲ့ ခွဲခြားနိုင်ဖို့
  • Buffering ကြောင့် fclose() ရဲ့ return value ကို ဘာကြောင့် စစ်ရလဲ ရှင်းပြနိုင်ဖို့
  • Windows မှာ text mode က '\n' ကို ဘယ်လို ပြောင်းလဲ သိပြီး binary data အတွက် "rb"/"wb" သုံးနိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

Variable တွေထဲက data က program ပိတ်လိုက်တာနဲ့ ပျောက်သွားပါတယ်။ ရမှတ်စာရင်း၊ setting တွေ၊ log တွေကို နောက်တစ်ခါ program ဖွင့်တဲ့အထိ ထားချင်ရင် disk ပေါ်က file ထဲ ရေးရပါတယ်။

C မှာ file နဲ့ အလုပ်လုပ်တာ စာကြည့်တိုက်က စာအုပ် ငှားသလိုပါပဲ။ fopen() နဲ့ ငှားတယ် (ဖတ်ဖို့လား၊ ရေးဖို့လား ပြောရတယ်)၊ fprintf() / fgets() နဲ့ သုံးတယ်၊ fclose() နဲ့ ပြန်အပ်တယ်။ စာအုပ် မရှိတာ၊ ငှားခွင့် မရတာ ဖြစ်နိုင်လို့ ငှားလို့ ရမရ အမြဲ စစ်ရပါတယ်။

Problem → Why → Solution

ပြဿနာ: ကိုယ့်စက်မှာ အလုပ်လုပ်ပြီး သူငယ်ချင်းစက်မှာ ပျက်တယ်

အောက်က program က settings.txt ရဲ့ ပထမ line ကို ဖတ်ပါတယ်။ Project folder ထဲကနေ run ရင် ကောင်းကောင်း အလုပ်လုပ်ပါတယ်။ သူငယ်ချင်းက တခြား folder ကနေ run လိုက်တော့ (ဒါမှမဟုတ် file နာမည် ပြောင်းသွားတော့) fopen() က NULL ပြန်ပါတယ်။ MinGW နဲ့ build ထားတဲ့ Windows program က garbage character တွေ print ထုတ်ပြီး Linux မှာတော့ ပုံမှန်အားဖြင့် segmentation fault နဲ့ crash ဖြစ်ပါတယ်။ -Wall -Wextra ကလည်း ဘာ warning မှ မပေးပါဘူး။

settings_bug.c
#include <stdio.h>

int main(void) {
    FILE *f = fopen("settings.txt", "r");   /* NULL if the file is missing */
    char line[100];
    fgets(line, sizeof line, f);            /* f may be NULL here */
    printf("first setting: %s", line);
    fclose(f);
    return 0;
}

ဘာကြောင့် ဖြစ်တာလဲ

"settings.txt" လို relative path က executable ရှိတဲ့ folder ကို မဟုတ်ဘဲ program ကို စတင်ခဲ့တဲ့ current working directory ကို အခြေခံပါတယ်။ File မတွေ့ရင် fopen() က NULL ပြန်ပြီး errno ထဲ အကြောင်းရင်း ထားပေးပေမယ့် program က မစစ်ဘဲ NULL ကို fgets() ဆီ ပို့လိုက်ပါတယ်။ fopen() ရဲ့ ရလဒ်ကို အမြဲ စစ်ပြီး perror("settings.txt") နဲ့ "settings.txt: No such file or directory" လို message ပြရင် ပြဿနာ ချက်ချင်း ရှင်းသွားပါတယ်။

Core concept: FILE stream၊ mode နဲ့ buffer

fopen(path, mode) က FILE * pointer (stream) ကို ပြန်ပေးပြီး read/write function တိုင်းက အဲဒီ pointer ကို လက်ခံပါတယ်။ File မရှိတာ၊ permission မရှိတာ၊ folder မရှိတာ၊ ဖွင့်ထားတဲ့ file များလွန်းတာတွေကြောင့် fopen() က NULL ပြန်နိုင်ပါတယ်။ "r" က ဖတ်ဖို့ (file ရှိရမယ်)၊ "w" က ရေးဖို့ (မရှိရင် ဖန်တီး၊ ရှိရင် fopen ချိန်မှာတင် content အားလုံး ဖျက်)၊ "a" က အဆုံးမှာ ဆက်ရေးဖို့ပါ။ "+" ထည့်ရင် ဖတ်ရော ရေးပါ ရပါတယ်။ File ကို ဖွင့်လိုက်ရုံနဲ့ ရှိပြီးသား content ပျက်မပျက်ကို mode က ဆုံးဖြတ်လို့ mode ကို သေချာ စဉ်းစားပြီး ရွေးပါ။

fprintf() နဲ့ fputs() က disk ပေါ် တိုက်ရိုက် မရေးပါဘူး၊ memory ထဲက buffer ထဲ အရင် စုထားပါတယ်။ Buffer ပြည့်တဲ့အခါ၊ fflush() ခေါ်တဲ့အခါ ဒါမှမဟုတ် fclose() ခေါ်တဲ့အခါမှ data က file ထဲ တကယ် ရောက်ပါတယ်။ ဒါကြောင့် disk ပြည့်နေရင် error က fprintf မှာ မဟုတ်ဘဲ fclose မှာမှ ပေါ်လာနိုင်လို့ fclose(f) == EOF ကို စစ်ရပါတယ်။ Program က fclose မခေါ်ဘဲ crash ဖြစ်သွားရင် buffer ထဲက နောက်ဆုံး data တွေ ဆုံးရှုံးနိုင်ပါတယ်။

ဖတ်တဲ့အခါ while (fgets(line, sizeof line, f) != NULL) ပုံစံကို သုံးပါ။ fgets က file ဆုံးတဲ့အခါရော error ဖြစ်တဲ့အခါပါ NULL ပြန်လို့ loop ပြီးမှ ferror(f) နဲ့ နှစ်ခုကို ခွဲပါ။ Line တိုင်းမှာ '\n' ပါနေသေးလို့ strcmp နဲ့ နှိုင်းယှဉ်ခင် ဖယ်ပါ။

Text mode နဲ့ binary mode က Linux နဲ့ macOS မှာ ဘာမှ မကွာပါဘူး။ Windows မှာတော့ text mode ("r"၊ "w") က ရေးတဲ့အခါ '\n' ကို "\r\n" byte နှစ်ခုအဖြစ် ပြောင်းပြီး ဖတ်တဲ့အခါ ပြန်ပြောင်းပါတယ်။ စာသားအတွက် အဆင်ပြေပေမယ့် ပုံ၊ zip၊ fwrite() နဲ့ ရေးတဲ့ number တွေလို byte အတိအကျ လိုတဲ့ data ကို ပျက်စေပါတယ်။ အဲဒီ data တွေအတွက် "rb"/"wb" ကို သုံးပြီး byte တွေကို မထိမခိုက် ထားပါ။

မှတ်ထားရမယ့် ပုံစံ: ငှား၊ သုံး၊ ပြန်အပ်
  1. fopen: mode နဲ့ ဖွင့်၊ NULL စစ်
  2. fprintf/fgets: buffer ကနေ ရေး/ဖတ်
  3. Loop ပြီးရင် ferror() နဲ့ စစ်
  4. fclose: buffer ကို flush၊ EOF စစ်

အဆင့်တိုင်းက fail ဖြစ်နိုင်လို့ return value တိုင်းကို စစ်ပါ။

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

c
FILE *f = fopen(path, mode);          /* "r", "w", "a", "rb", "wb", ... */
if (f == NULL) {
    perror(path);                       /* explains why */
    return EXIT_FAILURE;
}
/* fprintf(f, ...) / fgets(buf, sizeof buf, f) / fread / fwrite */
if (fclose(f) == EOF) {
    /* buffered data may not have reached the file */
}

ဖွင့်၊ NULL စစ်၊ သုံး၊ ပိတ်ပြီး ပိတ်တာ အောင်မြင်လား စစ်ဆိုတဲ့ အစီအစဉ်က file တိုင်းအတွက် တူပါတယ်။ ပြောင်းတာက mode နဲ့ အလယ်က read/write function တွေပဲ ဖြစ်ပါတယ်။

Example: ရမှတ်တွေကို ရေးပြီး ပြန်ဖတ်ခြင်း

ဒီ program က current directory ထဲမှာ scores-demo.txt ကို ဖန်တီးပြီး line သုံးကြောင်း ရေးကာ ပိတ်ပါတယ်။ ပြီးမှ ပြန်ဖွင့်ပြီး line တစ်ကြောင်းချင်း ဖတ်ကာ ပျမ်းမျှ ရမှတ် တွက်ပါတယ်။ နောက်ဆုံးမှာ remove() နဲ့ demo file ကို ပြန်ဖျက်လို့ ဘာမှ ကျန်မနေပါဘူး။ File ကို ဖွင့်ကြည့်ချင်ရင် remove line ကို comment ပိတ်ပါ။

scores_file.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    const char *path = "scores-demo.txt";   /* relative to the current directory */

    /* 1. Write: "w" creates the file, or empties it if it exists */
    FILE *out = fopen(path, "w");
    if (out == NULL) {
        perror(path);
        return EXIT_FAILURE;
    }
    fprintf(out, "Aye 82\n");
    fprintf(out, "Ko 91\n");
    fprintf(out, "Su 77\n");
    if (fclose(out) == EOF) {               /* flushes the buffer to the file */
        perror("fclose");
        return EXIT_FAILURE;
    }

    /* 2. Read it back, one line at a time */
    FILE *in = fopen(path, "r");
    if (in == NULL) {
        perror(path);
        return EXIT_FAILURE;
    }
    char line[100];
    int count = 0, total = 0;
    while (fgets(line, sizeof line, in) != NULL) {
        char name[32];
        int score;
        if (sscanf(line, "%31s %d", name, &score) == 2) {
            printf("%-4s %3d\n", name, score);
            total += score;
            count++;
        }
    }
    int status = EXIT_SUCCESS;
    if (ferror(in)) {                       /* NULL meant error, not end of file */
        perror("read");
        status = EXIT_FAILURE;
    }
    fclose(in);
    remove(path);                           /* delete the demo file again */

    if (count > 0) {
        printf("average: %.1f\n", (double)total / count);
    }
    return status;
}

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

  1. Open for writingFILE *out = fopen(path, "w");

    "w" က file မရှိရင် ဖန်တီးပြီး ရှိရင် ဒီ line မှာတင် content အဟောင်း အကုန် ဖျက်ပါတယ်။ ရှိပြီးသား data ကို ထိန်းထားချင်ရင် "a" ကို သုံးပါ။

  2. Report the reasonperror(path);

    errno ထဲက အကြောင်းရင်းကို stderr ဆီ "scores-demo.txt: Permission denied" လို ပုံစံနဲ့ ထုတ်ပေးပါတယ်။ ဘယ် file မှာ ဘာဖြစ်လဲ user ချက်ချင်း သိပါတယ်။

  3. Close and flushif (fclose(out) == EOF) { /* flushes the buffer to the file */

    fprintf သုံးခုက buffer ထဲမှာပဲ ရှိနေနိုင်လို့ fclose က တကယ် file ထဲ ရေးပါတယ်။ ဒီမှာ fail ရင် data က file ထဲ မရောက်တာ ဖြစ်နိုင်ပါတယ်။

  4. Read loopwhile (fgets(line, sizeof line, in) != NULL) {

    fgets က line တစ်ကြောင်းကို '\n' အပါ ဖတ်ပြီး file ဆုံးရင် NULL ပြန်လို့ loop က လိုသလောက်ပဲ ပတ်ပါတယ်။ sscanf နဲ့ name နဲ့ score ကို ခွဲထုတ်ပြီး %31s က name[32] ထက် မကျော်အောင် ကာကွယ်ပါတယ်။

  5. Error or end?if (ferror(in)) { /* NULL meant error, not end of file */

    Loop ပြီးတာက file ဆုံးလို့လား၊ read error ကြောင့်လား ဒီမှာ ခွဲပါတယ်။ Error ဆိုရင် program ရဲ့ exit status ကို EXIT_FAILURE ပြောင်းပါတယ်။

  6. Clean upremove(path); /* delete the demo file again */

    remove() က file ကို ဖျက်ပြီး အောင်မြင်ရင် 0 ပြန်ပါတယ်။ File ကို ပိတ်ပြီးမှ ဖျက်တာ သတိထားပါ၊ Windows မှာ ဖွင့်ထားတဲ့ file ကို ဖျက်လို့ မရတတ်ပါဘူး။

You should see
Aye   82
Ko    91
Su    77
average: 83.3

Mode တွေ အကျဉ်းချုပ်

ModeFile မရှိရင်File ရှိရင်
"r"NULL ပြန်အစကနေ ဖတ်
"w"ဖန်တီးContent အကုန် ဖျက်ပြီး ရေး
"a"ဖန်တီးအဆုံးမှာ ဆက်ရေး
"r+"NULL ပြန်ဖတ်/ရေး၊ မဖျက်
"w+" / "a+"ဖန်တီး"w" / "a" လိုပဲ၊ ဖတ်လည်း ရ
"wx" (C11)ဖန်တီးNULL ပြန် (overwrite မလုပ်)

Mode တိုင်းမှာ b ထည့်လို့ ရပါတယ် ("rb"၊ "wb"၊ "ab"၊ "r+b")။ C11 ရဲ့ "wx" ကို glibc နဲ့ Microsoft ရဲ့ UCRT က ထောက်ပံ့ပေမယ့် MinGW toolchain တချို့ သုံးတဲ့ msvcrt.dll အဟောင်းကတော့ "Invalid argument" နဲ့ ငြင်းတာကို w64devkit GCC 16 မှာ တွေ့ရပါတယ်။

Text mode နဲ့ binary mode ကို ကိုယ်တိုင် စမ်းကြည့်ခြင်း

အောက်က program က "one\ntwo\n" ကို "w" နဲ့ တစ်ခါ၊ "wb" နဲ့ တစ်ခါ ရေးပြီး disk ပေါ်က byte အရေအတွက်ကို "rb" နဲ့ ရေတွက်ပါတယ်။ Windows မှာ "w" က 10 bytes (newline တစ်ခုစီက \r\n ဖြစ်လို့)၊ "wb" က 8 bytes ပြပြီး Linux နဲ့ macOS မှာ နှစ်ခုလုံး 8 bytes ပါ။

text_vs_binary.c
#include <stdio.h>

/* Counts a file's bytes, reading in binary mode so nothing is translated. */
long count_bytes(const char *path) {
    FILE *f = fopen(path, "rb");
    if (f == NULL) {
        return -1;
    }
    long n = 0;
    while (fgetc(f) != EOF) {
        n++;
    }
    fclose(f);
    return n;
}

int main(void) {
    const char *path = "mode-demo.txt";
    const char *modes[] = {"w", "wb"};

    for (int i = 0; i < 2; i++) {
        FILE *f = fopen(path, modes[i]);
        if (f == NULL) {
            perror(path);
            return 1;
        }
        fputs("one\ntwo\n", f);
        if (fclose(f) == EOF) {
            perror("fclose");
            return 1;
        }
        printf("mode \"%s\": %ld bytes on disk\n", modes[i], count_bytes(path));
    }
    remove(path);
    return 0;
}

Binary data အတွက်တော့ fwrite() နဲ့ fread() ကို "wb"/"rb" နဲ့ တွဲသုံးပါ။ နှစ်ခုလုံး တကယ် ရေး/ဖတ်ခဲ့တဲ့ item အရေအတွက်ကို ပြန်ပေးလို့ မေးခဲ့တဲ့ အရေအတွက်နဲ့ နှိုင်းယှဉ်ပါ။ 2560 ရဲ့ byte တွေထဲမှာ 0x0A ('\n') ပါလို့ text mode နဲ့ Windows မှာ ရေးရင် ပျက်သွားမယ့် value မျိုးပါ။ ဒီလို int ကို တိုက်ရိုက် ရေးထားတဲ့ file က byte order နဲ့ int အရွယ်အစား တူတဲ့ စက်တွေကြားမှာပဲ ဖတ်လို့ရတာကိုလည်း မှတ်ထားပါ။

binary_numbers.c
#include <stdio.h>

int main(void) {
    const char *path = "numbers-demo.bin";
    int data[] = {10, 2560, -1, 42};
    size_t n = sizeof data / sizeof data[0];

    FILE *f = fopen(path, "wb");
    if (f == NULL) {
        perror(path);
        return 1;
    }
    size_t written = fwrite(data, sizeof data[0], n, f);
    if (fclose(f) == EOF || written != n) {
        perror("write");
        remove(path);
        return 1;
    }

    int back[4];
    f = fopen(path, "rb");
    if (f == NULL) {
        perror(path);
        return 1;
    }
    size_t got = fread(back, sizeof back[0], n, f);
    fclose(f);
    remove(path);

    printf("read %d of %d ints:", (int)got, (int)n);
    for (size_t i = 0; i < got; i++) {
        printf(" %d", back[i]);
    }
    printf("\n");
    return 0;
}

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

Real project တွေမှာ ဘယ်လို တွေ့ရလဲ

Server program တွေက event တိုင်းကို log file ထဲ "a" mode နဲ့ ဆက်ရေးပါတယ်၊ "w" နဲ့ ဖွင့်မိရင် restart လုပ်တိုင်း log အဟောင်းတွေ ပျောက်သွားမှာပါ။ Command-line tool တွေက config file ကို "r" နဲ့ ဖတ်ပြီး မရှိရင် perror နဲ့ ရှင်းရှင်း ပြောကာ default value တွေ သုံးပါတယ်။ ပုံ၊ အသံ၊ database page တွေလို binary format တွေကိုတော့ "rb"/"wb" နဲ့ fread/fwrite သုံးပြီး ကိုင်ပါတယ်။

Data ဆုံးရှုံးလို့ မရတဲ့ program တွေ (text editor၊ database) က file အဟောင်းကို တိုက်ရိုက် overwrite မလုပ်ပါဘူး။ File အသစ် (ဥပမာ notes.txt.tmp) ထဲ အရင်ရေး၊ fclose အောင်မြင်တာ စစ်ပြီးမှ rename() နဲ့ နာမည်ပြောင်းတဲ့ နည်းကို သုံးလေ့ရှိပါတယ်။ ဒါမှ ရေးနေတုန်း ဓာတ်အားပြတ်ရင်တောင် file အဟောင်း မပျက်ဘဲ ကျန်ပါတယ်။ ဒီလို နည်းတွေ အလုပ်ဖြစ်ဖို့ return value တိုင်းကို စစ်တာက အခြေခံပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. while (!feof(f)) နဲ့ loop ပတ်တာ

feof() က read တစ်ခု fail ပြီးမှ true ဖြစ်ပါတယ်။ while (!feof(f)) { fgets(line, ...); printf("%s", line); } လို့ ရေးရင် နောက်ဆုံး fgets က NULL ပြန်ပေမယ့် line ထဲမှာ အရင် value ကျန်နေလို့ နောက်ဆုံး line ကို နှစ်ခါ print ထုတ်ပါတယ် (GCC 16 နဲ့ "a\nb\n" file ကို စမ်းတော့ b နှစ်ခါ ထွက်ပါတယ်)။ fgets ရဲ့ return value ကိုပဲ loop condition လုပ်ပါ။

2. ရှိပြီးသား file ကို "w" နဲ့ ဖွင့်တာ

"w" က fopen ခေါ်တဲ့ အချိန်မှာတင် file ကို အလွတ်ဖြစ်အောင် ဖြတ်ပါတယ်၊ ဘာမှ မရေးရသေးခင် program crash ဖြစ်ရင်တောင် data ပျောက်ပြီးသား ဖြစ်ပါတယ်။ Log နဲ့ history တွေအတွက် "a"၊ ပြင်ဖို့ဆိုရင် "r+" ဒါမှမဟုတ် temporary file နည်းကို သုံးပါ။

3. fclose ကို မေ့တာ ဒါမှမဟုတ် မစစ်တာ

fclose မခေါ်ရင် buffer ထဲက data က file ထဲ မရောက်ဘဲ ကျန်နိုင်ပြီး loop ထဲမှာ file တွေ ဖွင့်ပြီး မပိတ်ရင် program က ဖွင့်လို့ရတဲ့ file အရေအတွက် ကန့်သတ်ချက်ကို ရောက်သွားပါတယ်။ fclose ရဲ့ EOF ရလဒ်ကို စစ်မှ disk ပြည့်တာလို write error ကို သိရပါတယ်။

4. Binary data ကို text mode နဲ့ ရေးတာ

Windows မှာ fwrite() ကို "w" mode နဲ့ သုံးရင် 0x0A byte တိုင်းရှေ့မှာ 0x0D ထပ်ဝင်လို့ ပုံ file တွေ ပျက်ပြီး int တွေ ပြန်ဖတ်ရင် value မှားပါတယ်။ Linux မှာ ပြဿနာ မပေါ်လို့ cross-platform bug အဖြစ် နောက်မှ ပေါ်လာတတ်ပါတယ်။ Binary data ကို အမြဲ "wb"/"rb" နဲ့ ဖွင့်ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • fopen တိုင်းနောက်မှာ NULL စစ်ပြီး perror(path) နဲ့ file နာမည်ပါ ပြပါ။
  • Loop condition ကို fgets / fread ရဲ့ return value နဲ့ ရေးပြီး loop ပြီးမှ ferror() စစ်ပါ။
  • ဖွင့်တဲ့ function ထဲမှာပဲ fclose လုပ်ပြီး ရလဒ်ကို စစ်ပါ။
  • Text မဟုတ်တဲ့ data အတွက် "b" mode သုံးပြီး program အတွက် ယာယီ file တွေကို ပြီးရင် remove() နဲ့ ဖျက်ပါ။

Mini challenge

Mini challenge: log file သေးသေးလေး

Line တစ်ကြောင်းကို log file အဆုံးမှာ ဆက်ရေးတဲ့ function နဲ့ file ကို line နံပါတ်တွေနဲ့ print ထုတ်တဲ့ function ရေးပါ။ မရှိတဲ့ file ကိုလည်း ကောင်းကောင်း ကိုင်တွယ်ရပါမယ်။

  • int append_line(const char *path, const char *text) — "a" mode သုံး၊ အောင်မြင်ရင် 0၊ fopen/fprintf/fclose တစ်ခုခု fail ရင် -1
  • int print_numbered(const char *path) — line တွေကို "1: ..." ပုံစံ print ထုတ်၊ line အရေအတွက် ဒါမှမဟုတ် error ဆို -1 ပြန်
  • main() က log-demo.txt ကို အရင်ဖျက်၊ started / processing / finished ကို append၊ print ထုတ်၊ ပြီးရင် ပြန်ဖျက်
  • မရှိတဲ့ no-such-file.txt ကို print_numbered() နဲ့ ခေါ်ပြီး -1 ရတာကို ပြရမယ်
tiny_log.c
#include <stdio.h>
#include <string.h>

/* Appends text and a newline. Returns 0 on success, -1 on error. */
int append_line(const char *path, const char *text) {
    // TODO: open in append mode and check for NULL
    // TODO: write the text and a newline, remember if it failed
    // TODO: fclose and check it too
    return -1;
}

/* Prints each line with its number. Returns the count, or -1 on error. */
int print_numbered(const char *path) {
    // TODO: open for reading, check for NULL
    // TODO: fgets loop; strip the newline; print "n: line"
    // TODO: ferror check, fclose, return the count
    return -1;
}

int main(void) {
    // TODO: remove("log-demo.txt") first, because "a" keeps old lines
    // TODO: append three events, then print_numbered and the count
    // TODO: remove the file, then try print_numbered("no-such-file.txt")
    return 0;
}

ရလဒ်: Program က ဒီလို print ထုတ်ရပါမယ်: 1: started 2: processing 3: finished lines in file: 3 no-such-file.txt could not be opened

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

fgets ရဲ့ newline ကို line[strcspn(line, "\n")] = '\0'; နဲ့ ဖယ်ပါ။ append_line() ထဲမှာ fprintf fail ရင်တောင် fclose ကို ခေါ်ပြီးမှ -1 ပြန်ပါ၊ မဟုတ်ရင် file handle ကျန်ခဲ့ပါမယ်။

အဖြေ ကြည့်ရန်
tiny_log.c
#include <stdio.h>
#include <string.h>

/* Appends text and a newline. Returns 0 on success, -1 on error. */
int append_line(const char *path, const char *text) {
    FILE *f = fopen(path, "a");
    if (f == NULL) {
        return -1;
    }
    int ok = fprintf(f, "%s\n", text) >= 0;
    if (fclose(f) == EOF) {
        ok = 0;
    }
    return ok ? 0 : -1;
}

/* Prints each line with its number. Returns the count, or -1 on error. */
int print_numbered(const char *path) {
    FILE *f = fopen(path, "r");
    if (f == NULL) {
        return -1;
    }
    char line[200];
    int n = 0;
    while (fgets(line, sizeof line, f) != NULL) {
        line[strcspn(line, "\n")] = '\0';
        printf("%d: %s\n", ++n, line);
    }
    int failed = ferror(f);
    fclose(f);
    return failed ? -1 : n;
}

int main(void) {
    const char *path = "log-demo.txt";
    remove(path);                         /* start fresh: "a" keeps old lines */

    const char *events[] = {"started", "processing", "finished"};
    for (int i = 0; i < 3; i++) {
        if (append_line(path, events[i]) != 0) {
            perror(path);
            return 1;
        }
    }
    int lines = print_numbered(path);
    if (lines < 0) {
        perror(path);
        return 1;
    }
    printf("lines in file: %d\n", lines);
    remove(path);

    if (print_numbered("no-such-file.txt") < 0) {
        printf("no-such-file.txt could not be opened\n");
    }
    return 0;
}

Quick Quiz 1 / 3

Program restart လုပ်တိုင်း log အဟောင်းတွေ ကျန်နေစေပြီး line အသစ်ကို အဆုံးမှာ ထည့်ချင်တယ်။ ဘယ် mode သုံးရမလဲ။

Quick Quiz 2 / 3

while (!feof(f)) { fgets(line, sizeof line, f); printf("%s", line); } ကို "a\nb\n" ပါတဲ့ file နဲ့ run ရင် ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

Windows မှာ fputc('\n', f) ကို "w" mode နဲ့ ဖွင့်ထားတဲ့ file ထဲ ရေးရင် disk ပေါ်မှာ ဘာ byte တွေ ရောက်မလဲ။

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

  • fopen() က NULL ပြန်နိုင်လို့ အမြဲ စစ်ပြီး perror() နဲ့ အကြောင်းရင်း ပြပါ၊ relative path က current working directory ကို အခြေခံပါတယ်။
  • "w" က ဖွင့်တာနဲ့ file ကို ဖျက်ပြီး "a" က အဆုံးမှာ ဆက်ရေးပါတယ်၊ text မဟုတ်တဲ့ data အတွက် "b" ထည့်ပါ။
  • fgets ရဲ့ return value နဲ့ loop ပတ်ပြီး ferror() နဲ့ error ကို ခွဲပါ၊ while (!feof(f)) ကို မသုံးပါနဲ့။
  • Write တွေက buffer ထဲ အရင်ရောက်လို့ fclose() ကို အမြဲ ခေါ်ပြီး EOF ပြန်မပြန် စစ်ပါ။

GNU C compiler diagnostics — GNU Project

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

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

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