การอ่านไฟล์
การอ่านไฟล์ .txt
ifstream file("datalogger.txt",ios::in);ตัวอย่างที่4: การอ่านข้อมูลจากไฟล์
#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;
}
ตัวอย่างที่5: การอ่านข้อมูลจากไฟล์และเช็คไฟล์
Last updated
Was this helpful?