ไฟล์นึงเก็บข้อมูลแบบ struct (พวก ID,productName,price) ไว้หลายข้อมูล
พอเปิดไฟล์แล้วมาเก็บใน array struct แล้วแสดงผลกลับเป็นภาษาต่างดาว
ขอคำแนะนำหน่อยครับ
void readFile(){
/* fstream myFile("Product.txt",ios::binary|ios::in);
myFile.read(reinterpret_cast<char *>(&st),sizeof(student));
myFile.close();*/ // ใช้ตามข้างบนอ่านออกแต่ได้แต่ข้อมูลเดียว ถ้ามีเกิน 1 จะค้างไปเลย
int maxValue = 20;
int i = 0;
student s[maxValue];
ifstream inFile("Product.txt");
if(inFile.fail()){
cout<<"Unable to open file"<<endl;
}else{
cout<<"File opened"<<endl;
while(!inFile.eof() && i < maxValue){
getline(inFile,s[i].productID);
getline(inFile,s[i].productName);
getline(inFile,s[i].price);
i++;
}
}
inFile.close();
cout<<"File closed"<<endl;
for(int j = 0; j < i; j++){
cout<<s[j].prodectID<<" "<<s[j].productName<<" "<<s[j].price<<endl;
}
}
C++ อ่านไฟล์ที่เก็บแบบ Struct แล้วอ่านไม่ออก
พอเปิดไฟล์แล้วมาเก็บใน array struct แล้วแสดงผลกลับเป็นภาษาต่างดาว
ขอคำแนะนำหน่อยครับ
void readFile(){
/* fstream myFile("Product.txt",ios::binary|ios::in);
myFile.read(reinterpret_cast<char *>(&st),sizeof(student));
myFile.close();*/ // ใช้ตามข้างบนอ่านออกแต่ได้แต่ข้อมูลเดียว ถ้ามีเกิน 1 จะค้างไปเลย
int maxValue = 20;
int i = 0;
student s[maxValue];
ifstream inFile("Product.txt");
if(inFile.fail()){
cout<<"Unable to open file"<<endl;
}else{
cout<<"File opened"<<endl;
while(!inFile.eof() && i < maxValue){
getline(inFile,s[i].productID);
getline(inFile,s[i].productName);
getline(inFile,s[i].price);
i++;
}
}
inFile.close();
cout<<"File closed"<<endl;
for(int j = 0; j < i; j++){
cout<<s[j].prodectID<<" "<<s[j].productName<<" "<<s[j].price<<endl;
}
}