开发者

Example for file input to structure members?

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


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


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜