> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/c-c++-for-embedded-programming/data-logger/undefined.md).

# การสร้างไฟล์และเขียนไฟล์

## การสร้างไฟล์.txt

สำหรับการเริ่มต้นใช้งานให้ทำการ include ไลบารี่ fstream  &#x20;

```cpp
#include <fstream>
```

สามารถสร้างไฟล์ .txt โดยเรียกใช้ class "ofstream" ดังนี้

```cpp
//class ชื่อObject("ชื่อไฟล์.txt")
ofstream file("datalogger.txt");
file.close();
```

หรือ

```cpp
//class ชื่อObject
ofstream file;
file.open("datalogger.txt");
file.close();
```

### ตัวอย่างที่ 1: การสร้างไฟล์

สร้าง folder ชื่อว่า “รหัสนิสิต\_lab11\_datalogger” และสร้างไฟล์โปรแกรม c++ ชื่อว่า “รหัสนิสิต\_lab11\_datalogger.cpp” ลงใน folder ในโปรแกรมกำหนดให้สร้างไฟล์.txt ชื่อว่า datalogger.txt

```bash
mkdir รหัสนิสิต_lab11_datalogger
cd รหัสนิสิต_lab11_datalogger
```

ใส่ code ด้านล่างลงไฟล์โปรแกรม โดยใช้คำสั่ง cat

```bash
cat >> รหัสนิสิต_lab11_datalogger.cpp
```

code

```cpp
// function create file .txt 
#include <iostream>
#include <fstream>
using namespace std; 
void createfile_txt(){ // ฟังก์ชันสำหรับการสร้างไฟล์
    ofstream file;
    file.open("datalogger.txt"); //สร้างไฟล์.txt
    file.close();
   }

int main(){
    createfile_txt();
    return 0;
}
```

กดคีย์บอร์ด Ctrl+z เพื่อออจากไฟล์ แล้วใส่คำสั่ง compile code “รหัสนิสิต\_lab11\_datalogger.cpp” โดย กำหนดให้ไฟล์ output ชื่อว่า “createfile\_txt”และทดลองรันโปรแกรม

```bash
g++ -o createfile_txt รหัสนิสิต_lab11_datalogger.cpp
./createfile_txt
```

ผลลัพท์ ที่ได้ดังภาพด้านล่าง

![](https://paper-attachments.dropbox.com/s_8D36E59B9CB6FC0FE03D6E052F4EB234A482D1B3C08E2812573EA61EC15BA079_1629012236609_image.png)

## การสร้าง และเขียนข้อมูลลงไฟล์ .txt

เราสามารถสร้างไฟล์เขียนข้อมูลไปในไฟล์ได้เลย สามารถเขียนในลักษณะเดียวกับกับคำสั่งการแสดงค่า cout โดยแทนด้วยชื่อฟังก์ชันที่เราสร้างขึ้นก่อนหน้า ในที่นี้ใช้ชื่อว่า file&#x20;

cout สำหรับแสดงผลบนหน้าจอ

```cpp
#include <iostream>
using namespace std; 

int main(){
    cout<<"hello" <<endl;
    file.close();
    return 0;
}
```

ofstream สำหรับเขียนค่าลงไฟล์

```cpp
#include <iostream>
#include <fstream>
using namespace std;

int main(){
  ofstream file;
  file.open("datalogger.txt");
  file<<"hello"<<endl; 
  file.close();
  return 0;
}
```

ตัวอย่างการใช้ cout แสดงข้อมูลตัวเลข 1 ถึง 5 ทีละบรรทัด

```cpp
#include <iostream>
#include <fstream>
using namespace std; 

void print(){
     for(int i=1;i<=5;i++){
        cout<<i<<endl;
     }
}

int main(){
    print();
    return 0;
}
```

compile code กำหนดให้ไฟล์ output ชื่อว่า “print”

```cpp
g++ -o print รหัสนิสิต_lab11_datalogger.cpp
./print
```

ผลลัพท์

![](https://paper-attachments.dropbox.com/s_8D36E59B9CB6FC0FE03D6E052F4EB234A482D1B3C08E2812573EA61EC15BA079_1633331616650_image.png)

### ตัวอย่างที่ 2: การสร้างและเขียนข้อมูลลงไฟล์&#x20;

การเขียนโปรแกรมสำหรับสร้างไฟล์และเขียนข้อมูลลงไฟล์ .txt

```cpp
#include <iostream>
#include <fstream>
using namespace std; 
void create_and_W_txt(){
    ofstream file;
    file.open("datalogger.txt");// สร้างไฟล์ 
    for(int i=1;i<=5;i++){  
     file<<i<<endl;  //เขียนข้อมูลตัวเลข 1 ถึง 10 ทีละบรรทัด 
     }
    file.close();
}
int main(){
    create_and_W_txt();
    return 0;
}
```

compile code กำหนดให้ไฟล์ output ชื่อว่า “create\_and\_W\_txt”

```cpp
g++ -o create_and_W_txt รหัสนิสิต_lab11_datalogger.cpp
./create_and_W_txt
```

ผลลัพท์

![](https://paper-attachments.dropbox.com/s_8D36E59B9CB6FC0FE03D6E052F4EB234A482D1B3C08E2812573EA61EC15BA079_1633333876156_image.png)

## การเขียนข้อมูลลงไฟล์ที่มีอยู่แล้ว

เราสามารถใช้คำสั่ง append จะเช็คว่ามีไฟล์อยู่ไหมถ้ามีจะทำการเขียนข้อมูลต่อจากเดิมแต่ถ้าไม่มีจะทำการสร้างไฟล์ดังกล่าวขึ้นมาแล้วทำการเขียนข้อมูลลงไป\
&#x20;คำสั่ง append&#x20;

```cpp
ofstream file("datalogger.txt", ios:app); // app = append 
```

### &#x20;ตัวอย่างที่3: การเขียนข้อมูลลงไฟล์ที่มีอยู่แล้ว

```cpp
#include <iostream>
#include <fstream>
using namespace std; 

 void write_app(){
    ofstream file;
    file.open("datalogger.txt",ios::app);  
    for(int i=6;i<=10;i++){
        file<<i<<endl
    }
    file.close();
 }
int main(){
    write_app();
    return 0;
}
```

&#x20;  compile code กำหนดให้ไฟล์ output ชื่อว่า “wirte\_app”

```cpp
 g++ -o write_app รหัสนิสิต_lab11_datalogger.cpp$./write_app
```

&#x20; ผลลัพท์

![](https://paper-attachments.dropbox.com/s_8D36E59B9CB6FC0FE03D6E052F4EB234A482D1B3C08E2812573EA61EC15BA079_1633334743288_image.png)

Update: May 2023

Author: Soontree Jaikhong (AIC-Researcher)

Author: Thanaluk Pranekunakol (AIC-Researcher)
