结构成员的文件输入示例?

发布于 2024-11-03 02:14:47 字数 816 浏览 3 评论 0原文

我有以下结构:

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

我有一个文件,将输入 10 种不同的产品,每种产品包含一个项目、详细信息和成本。我尝试使用 inFile.getline 输入它,但它不起作用。谁能给我一个如何做到这一点的例子?我将不胜感激。

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

570314,SanDisk Sansa Clip 8 GB MP3 Player Black,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);
    }
}

I have the following structure:

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

I have a file that will input 10 different products that each contain an item, details, and cost. I have tried to input it using inFile.getline but it just doesn't work. Can anyone give me an example of how to do this? I would appreciate it.

Edit
The file contains 10 lines that look like this:

570314,SanDisk Sansa Clip 8 GB MP3 Player Black,55.99

Can you provide an example please.

Edit
Sorry guys, I am new to C++ and I don't really understand the suggestions. This is what I have tried.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泅渡 2024-11-10 02:14:47

这是另一种方法,使用 fstream 读取文件并使用 getline() 读取文件中的每一行。由于其他帖子,该行本身的解析被故意省略已经这样做了。

在读取每一行并将其解析为产品信息后,应用程序将其存储在向量上,因此可以在内存中访问所有产品。

#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++ 人员可能不喜欢 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);
}

This is another approach that uses fstream to read the file and getline() to read each line on the file. The parsing of the line itself was left out on purpose since other posts have already done that.

After each line is read and parsed into a productInfo, the application stores it on a vector, so all products could be accessed in memory.

#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: I decided to take a shot at parsing the line and wrote the code below. Some C++ folks might not like the strtok() method of handling things but there it is.

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);
}
雄赳赳气昂昂 2024-11-10 02:14:47

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

int i;
输入文件>>我;

如果它是二进制的,您可以将其读入 &your_struct。

It depends on what's in the file? If it's text, you can use the redirect operator on a file input stream:

int i;
infile >> i;

If it's binary, you can just read it in to &your_struct.

夏九 2024-11-10 02:14:47

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

然后继续执行此操作,直到到达文件末尾。

You have to
0) Create a new instance of productInfo, pinfo;
1) read text (using getline) to the first comma (','), convert this string to an int, and put it into pinfo.item.
2) read text to the next comma and put it into pinfo.details;
3) read text to the endline, convert the string to a double, and put it into pinfo.cost.

Then just keep doing this until you reach the end of the file.

烈酒灼喉 2024-11-10 02:14:47

以下是我使用 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

Here is how I would use getline. Note that I use it once to read from the input file, and then again to chop that line at ",".

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.: This program is buggy if the input is

99999,"The Best Knife, Ever!",16.95
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文