结构成员的文件输入示例

Example for file input to structure members?

本文关键字:输入 文件 成员 结构      更新时间:2023-10-16

我有以下结构:

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

我有一个文件,它将输入10个不同的产品,每个产品包含一个项目、详细信息和成本。我试着用inFile输入它。Getline,但它就是不起作用。有人能给我举个例子吗?我很感激。

编辑该文件包含10行,如下所示:

570314,SanDisk Sansa Clip 8gb 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);
    }
}

这是另一种使用fstream读取文件并使用getline()读取文件上的每一行的方法。这一行的解析是故意省略的,因为其他文章已经这样做了。

在读取每行并解析为productInfo之后,应用程序将其存储在vector中,因此可以在内存中访问所有产品。

#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;
}

EDIT:我决定试着解析这行代码,并编写了下面的代码。一些c++爱好者可能不喜欢strtok()处理事情的方法,但它就是这样。

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

这取决于文件中的内容?如果是文本,可以在文件输入流上使用重定向操作符:

int我;

如果是二进制的,你可以直接读入到&your_struct.

你必须这么做0)创建productInfo, pinfo的新实例;1)读取文本(使用getline)到第一个逗号(','),将该字符串转换为int型,并将其放入pinfo.item中。2)阅读文本到下一个逗号,并将其放入pinfo.details中;3)将文本读入尾行,将字符串转换为双精度类型,并放入pinfo.cost.

然后一直这样做,直到你到达文件的末尾。

我将如何使用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;

的被害者。如果输入是

,这个程序就会出错
99999,"The Best Knife, Ever!",16.95