我有以下結構:文件輸入到結構成員的例子?
struct productInfo
{
int item;
string details;
double cost;
};
我有一個文件,將輸入10種不同的產品,每個都包含一個項目,細節和成本。我試圖用inFile.getline輸入它,但它不起作用。任何人都可以給我一個如何做到這一點的例子嗎?我會很感激。
編輯 該文件包含10行看起來像這樣:
570314,SanDisk公司的Sansa剪輯8 GB MP3播放器黑色,55.99
你能提供一個例子吧。
編輯 對不起,我是C++的新手,我不太瞭解這些建議。這是我嘗試過的。
void readFile(ifstream & inFile, productInfo products[])
{
inFile.ignore(LINE_LEN,'\n'); // The first line is not needed
for (int index = 0; index < 10; index++)
{
inFile.getline(products[index].item,SIZE,DELIMETER);
inFile.getline(products[index].details,SIZE,DELIMETER);
inFile.getline(products[index].cost,SIZE,DELIMETER);
}
}
什麼都不起作用?向我們展示您如何嘗試讀取文件內容的代碼。 – Mahesh 2011-04-24 02:01:39
@Mahesh我很確定沒有這樣的代碼:) – 2011-04-24 02:29:41
我剛剛添加了我嘗試過的代碼。 – user722049 2011-04-24 14:56:48