> 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/c++.md).

# การเก็บข้อมูลกับTime stamp

## การเก็บข้อมูลกับTime stamp

เมื่อข้อมูลมีการเข้ามา เราจำเป็นจะต้องจดเวลาที่ได้รับข้อมูลมาเพื่อที่จะได้นำไปสืบค้นได้ง่าย หรือสามารถนำไปนำเสนอในรูปแบบของกราฟที่เป็น time series ได้ โดยการจดเวลาลงไปพร้อมกับข้อมูลนี้เรียกว่าการทำ time stamp เราสามารถใส่ time stamp ลงไปบนข้อมูลได้อย่างง่ายโดยการเพิ่มไลบรารี่     &#x20;

```cpp
#include <ctime>
```

&#x20;      ซึ่งจะเป็นไลบรารี่ที่อ้างอิงกับเวลาที่อยู่บนเครื่องของเรา เราสามารถดูรายละเอียดของแต่ละฟังก์ชันได้ [ที่นี่](https://www.tutorialspoint.com/cplusplus/cpp_date_time.htm) ตัวอย่างด้านล่างนี้จะเป็นตัวอย่างการสร้างไฟล์และจำลองข้อมูลที่เข้ามาโดยมีเวลาติดมาด้วย

### &#x20;ตัวอย่างที่6: เขียนข้อมูลและใส่เวลาที่เก็บข้อมูล

```cpp
#include <iostream>
#include <fstream>
#include <ctime> // สำหรับอ่านค่าเวลา
using namespace std;
void file_create_ts(){ //ฟังก์ชั่นสร้างไฟล์
    ofstream file;
    file.open("datalogger_timestamp.txt");// สร้างไฟล์ 
    time_t now = time(0); //สร้างตัวแปรที่เก็บค่าเวลาปัจจุบัน
    for(int i=1;i<=5;i++){  
     file<<i<<" : "<<ctime(&now);  //เขียนข้อมูลตัวเลข 1 ถึง 5 ทีละบรรทัด และต่อด้วยเวลา
     }
    file.close();
}
int main(){
    file_create_ts();
    return 0;
}
```

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

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

ผลลัพธ์

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

### ตัวอย่างที่7: อ่านข้อมูลใส่ไฟล์โดยใช้ getline()&#x20;

เราสามารถใช้ getline() ฟังก์ชั่นที่อยู่ในไลบารี่ของ string ได้ดังตัวอย่างด้านล่าง

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

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

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

ผลลัพท์

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

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/c++.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.
