> 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-1.md).

# การอ่านไฟล์

## การอ่านไฟล์ .txt

จากไลบารี่ fstream เราสามารถใช้ class ifstream ในการอ่านข้อมูลจากไฟล์ .txt ได้

```cpp
ifstream file("datalogger.txt",ios::in);
```

โดยการอ่านค่าไฟล์จะต้องประกาศตัวแปรสำหรับนรับค่าจากไฟล์ที่เราได้ทำการมาด้วย&#x20;

### ตัวอย่างที่4: การอ่านข้อมูลจากไฟล์

```cpp
#include <iostream>
#include <fstream>
using namespace std;
void read_data(){
    int data; //กำหนดตัวแปรสำหรับรับค่าที่อ่านจากไฟล์มาแสดงผล
    ifstream file("datalogger.txt"); //รับข้อมูลจากไฟล์.txt
        while (file>>data){ // ถ้าไม่มีข้อมูลจากไฟล์ส่งเข้าตัวแปร data โปรแกรมจะหยุดอ่าน
          cout << data<<endl;
        } 
        file.close();
    }                          
int main(){
   read_data();
  return 0;
}
```

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

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

ผลลัพท์

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

### ตัวอย่างที่5: การอ่านข้อมูลจากไฟล์และเช็คไฟล์

\
จากตัวอย่างเป็นการอ่านค่าจากไฟล์ datalogger.txt มาเก็บไว้ในตัวแปร data แล้วแสดงผลด้วยคำสั่ง cout ซึ่งจะเป็นการอ่านข้อมูลแค่เพียงบรรทัดเดียว ถ้าต้องการให้อ่านข้อมูลบรรถัดไปจะต้องทำการเขียนโปรแกรมให้อ่านค่าไปเลื่อนๆ&#x20;

{% hint style="warning" %}
ในกรณีที่ไฟล์ที่ต้องการ อ่านแต่ไม่มีไฟล์อยู่ c++ จะไม่เตือนดังนั้นให้ใส่ code สำหรับตรวจสอบไฟล์ด้วย
{% endhint %}

```cpp
#include <iostream>
#include <fstream>
using namespace std;
void read_n_check_file(){
    int data; //กำหนดตัวแปรสำหรับรับค่าที่อ่านจากไฟล์มาแสดงผล
    ifstream file; 
    file.open("datalogger.txt"); //รับข้อมูลจากไฟล์.txt
    if(!file.is_open()){   // ถ้าไม่สามารถเปิดไฟล์ได้ใ้แสดงข้อความ
        cout<< "can't open file\n"; 
    }else{
        while (file>>data){
          cout << data<<endl;
        }
        file.close();
    }                        
}
int main(){
   read_n_check_file();
  return 0;
}
```

compile code กำหนดให้ไฟล์ output ชื่อว่า “read\_n\_check\_file”

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

\
Update: May 2023

Author: Soontree Jaikhong (AIC-Researcher)

Author: Thanaluk Pranekunakol (AIC-Researcher)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/data-logger/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
