1. 程式人生 > >C語言-自我學習-讀寫檔案

C語言-自我學習-讀寫檔案

人懶要多記筆記

C語言讀寫檔案

End Of File,在電腦的術語縮寫通常為 EOF,在作業系統決定資料源無更多的資料可讀取。資料源通常稱為檔案或串流。

關鍵物件

fopen ->應該是指向Fileopen物件

fprintf ->應該是指向寫入物件,

fclose ->file close

寫檔案

    FILE *pFILE = fopen("./out.txt", "w");

    fprintf(pFILE, "1adfkdfkasdkfkasd");

    fclose(pFILE);

讀檔案

    FILE *pFILE = fopen("./a.txt", "r");

    if (pFILE == nullptr) {
        printf("不能讀");
        return;
    }
    int ch;
    // 獲取讀寫狀態
    ch = fgetc(pFILE);
    // 如果沒有更多資料可以讀寫,則停止
    while (ch != EOF) {
        // 獲取讀寫狀態
        int i = fgetc(pFILE);
        if (i == EOF) {
            break;
        }
        putchar(ch);
        ch = i;
    }
    printf("%c", ch);
    fclose(pFILE);