2011-04-24 32 views
0

我有以下結構:文件輸入到結構成員的例子?

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); 
    } 
} 
+1

什麼都不起作用?向我們展示您如何嘗試讀取文件內容的代碼。 – Mahesh 2011-04-24 02:01:39

+2

@Mahesh我很確定沒有這樣的代碼:) – 2011-04-24 02:29:41

+0

我剛剛添加了我嘗試過的代碼。 – user722049 2011-04-24 14:56:48

回答

0

這取決於文件內容?如果是文本,則可以在文件輸入流上使用重定向運算符:

int i; infile >> i;

如果是二進制文件,您可以將其讀入到your_struct的&中。

0

您必須 0)創建一個新的productInfo實例,pinfo; 1)將文本(使用getline)讀取到第一個逗號(','),將此字符串轉換爲int,並將其放入pinfo.item。 2)將文本讀到下一個逗號並將其放入pinfo.details中; 3)將文本讀取到endline,將該字符串轉換爲double,並將其放入pinfo.cost中。

然後,只要繼續這樣做,直到達到文件末尾。

0

這裏是我將如何使用getline。請注意,我使用它從輸入文件中讀取一次,然後再次在「,」處截斷該行。

ostream& operator>>(istream& is, productInfo& pi) 
{ 
    string line; 
    getline(is, line); // fetch one line of input 

    stringstream sline(line); 
    string item; 

    getline(sline, item, ','); 
    stringstream(item) >> pi.item; // convert string to int 

    getline(sline, item, ','); 
    pi.details = item;    // string: no conversion necessary 

    getline(sline, item); 
    stringstream(item) >> pi.cost; // convert string to double 
    return is; 
} 

// usage: 
// productInfo pi; ifstream inFile ("inputfile.txt"); inFile >> pi; 

N.b:這個程序是越野車,如果輸入的是

99999,"The Best Knife, Ever!",16.95 
+0

爲什麼要插入一個stringstream對象? – 2011-04-24 02:33:51

+1

@Matt - 第一個'getline' /'stringstream'允許我聲明這個'>>'將消耗恰好一行輸入,而不管錯誤,輸入格式錯誤,&c。 – 2011-04-24 03:07:36

1

這是一個使用fstream讀取文件和getline()讀取該文件的每一行的另一種方法。線路本身的解析被排除在外since other posts已經做到了。

每一行被讀取並解析爲一個productInfo後,應用程序將其存儲在一個向量中,因此所有產品都可以在內存中訪問。

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <iterator> 
#include <string> 

using namespace std; 

struct productInfo 
{ 
    int item; 
    string details; 
    double cost; 
}; 

int main() 
{ 
    vector<productInfo> product_list; 

    ifstream InFile("list.txt"); 
    if (!InFile) 
    { 
     cerr << "Couldn´t open input file" << endl; 
     return -1; 
    } 

    string line; 
    while (getline(InFile, line)) 
    { // from here on, check the post: How to parse complex string with C++ ? 
     // https://stackoverflow.com/questions/2073054/how-to-parse-complex-string-with-c 
     // to know how to break the string using comma ',' as a token 

     cout << line << endl; 

     // productInfo new_product; 
     // new_product.item = 
     // new_product.details = 
     // new_product.cost = 
     // product_list.push_back(new_product); 
    }  


    // Loop the list printing each item 

    // for (int i = 0; i < product_list.size(); i++) 
    //  cout << "Item #" << i << " number:" << product_list[i].item << 
    //        " details:" << product_list[i].details << 
    //        " cost:" << product_list[i].cost << endl; 

} 

編輯:我決定採取射擊在解析線和寫下面的代碼。一些C++人可能不喜歡處理事物的方法,但它存在。

string line; 
while (getline(InFile, line)) 
{ 
    if (line.empty()) 
     break; 

    //cout << "***** Parsing: " << line << " *****" << endl; 

    productInfo new_product; 
    // My favorite parsing method: strtok() 
    char *tmp = strtok(const_cast<char*>(line.c_str()), ","); 
    stringstream ss_item(tmp); 
    ss_item >> new_product.item; 
    //cout << "item: " << tmp << endl; 
    //cout << "item: " << new_product.item << endl; 

    tmp = strtok(NULL, ","); 
    new_product.details += tmp; 
    //cout << "details: " << tmp << endl; 
    //cout << "details: " << new_product.details << endl; 

    tmp = strtok(NULL, " "); 
    stringstream ss_cost(tmp); 
    ss_cost >> new_product.cost; 
    //cout << "cost: " << tmp << endl; 
    //cout << "cost: " << new_product.cost << endl; 

    product_list.push_back(new_product); 
} 
+0

更新後的答案來處理解析。 – karlphillip 2011-04-24 02:32:54

+0

好的。我會盡快更新代碼。謝謝@Rob – karlphillip 2011-04-24 03:15:04