C ++:读取分隔文件并存储在结构中

时间:2014-11-21 07:11:43

标签: c++ vector structure

我是编程新手,所以我可以使用我的代码帮助。我正在尝试读取具有以下格式的数据的现有文件:

2012-09-22|microsoft|0|102.36|100
2012-09-22|apple|0|203.12|100
2012-10-05|ibm|0|141.96|100
2012-10-05|boeing|0|123.65|100
2012-11-03|sears|0|23.36|100
2012-11-29|toyota|0|78.45|100

我发布了我的所有代码,但相关的函数是void loadData(事务allTransaction);功能。我从这里找到的多个例子中一起破解了这段代码,但它并不适合我。我试图读取数据,然后填充allTransaction结构的变量。然后我想将该结构推送到数据向量。我想循环每一行,直到我到达文件的末尾。看起来我没有得到填充结构的值。请帮忙!

我现在只在校园里做了2个月的编程,所以请保持对noob级别的回复。

由于

#define _CRT_SECURE_NO_DEPRECATE // Declaration to use deprecated code in Visual Studio.

#include <iostream> // Include the iostream standard file.
#include <string> // Include the string library.
#include <fstream> // Include to manipulate files.
#include <vector> // Include to use vectors.
#include <sstream> // Include to use stringstream.
#include <ctime> // Include to calculate time.

using namespace std;

// Create transactions structure
struct transaction
{
    /*int user = 0;*/
    string date = "";
    string stock = "";
    bool sale = 0;
    double price = 0.0;
    int shares = 0;
};

// Create vector
vector <transaction> data;

// Function declarations
void loadData(transaction allTransaction);
void buyShares(transaction allTransaction);
void sellShares(transaction allTransaction);
void storeData(transaction allTransaction);

int main()
{

    // Declare variables
    transaction allTransaction; // Declaring a structure.

    loadData(allTransaction);

    cout << "Date: " << allTransaction.date <<
        "Name: " << allTransaction.stock <<
        "Sale?: " << allTransaction.sale <<
        "Price: " << allTransaction.price <<
        "Shares: " << allTransaction.shares;

    //Pause program
    cout << "Press Enter To Continue..." << endl;
    cin.ignore();
    cin.get();
    return 0;

}

// Load text file function
void loadData(transaction allTransaction)
{

    string line;
    ifstream myfile("data.txt");
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            istringstream f(line);
            string s;
            while (getline(f, s, '|')) {
                myfile >> allTransaction.date >>
                    allTransaction.stock >>
                    allTransaction.sale >>
                    allTransaction.price >>
                    allTransaction.shares;
                data.push_back(allTransaction);
            }
            myfile.close();
        }
    }
    else std::cout << "Unable to open file";

}

// Buy stock function
void buyShares(transaction allTransaction)
{
    allTransaction.sale = 0; // Set sale boolean to 0, indicating a buy.
    storeData(allTransaction); // Call storeData function to store the data.
}

// Sell stock function
void sellShares(transaction allTransaction)
{
    allTransaction.sale = 1; // Set sale boolean to 1, indicating a sell.

    // Need function to check shares on hand.

    storeData(allTransaction); // Call storeData function to store the data.
}

void storeData(transaction allTransaction)
{
    ofstream myfile;
    myfile.open("data.txt", ios::out | ios::app);
    myfile << allTransaction.date << "|" << allTransaction.stock << "|" << allTransaction.sale << "|" <<
        allTransaction.price << "|" << allTransaction.shares << '\n';
    myfile.close();

    data.push_back(allTransaction);
}

1 个答案:

答案 0 :(得分:1)

在我看来,有一件事就是这个循环:

while (getline(f, s, '|')) {
    myfile >> allTransaction.date >>
        allTransaction.stock >>
        allTransaction.sale >>
        allTransaction.price >>
        allTransaction.shares;
    data.push_back(allTransaction);
}

首先你从istingstream f读取分隔符'|' (这是一件好事)但是在while循环体内你再次从文件中读取(这不是你想要的)。你可能会这样做。

    std::vector<std::string> v;
    while (getline(f, s, '|')) {
      v.push_back(s);
    }
    if(v.size()!=5) {
      std::cerr << "expected 5 values" << std::endl;
      return false; // or throw an exception etc...
    }
// At this point you have a vector with the strings 
// delimited by '|' ready for processing
// into your allTransaction class