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

标签 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(transaction allTransaction);功能。我从我在这里找到的多个示例中一起破解了这段代码,但它对我不起作用。我试图读入数据,然后填充 allTransaction 结构的变量。然后我想将该结构推送到数据 vector 。我想循环每一行,直到到达文件末尾。看起来我没有得到填充结构的值。请帮忙!

我在学校只做了 2 个月的这个编程,所以请保持你的回答是菜鸟水平。

谢谢

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

最佳答案

突然出现在我眼中的一件事是这个循环:

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

关于C++:读取带分隔符的文件并存储在结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27055973/

相关文章:

c - 如何使用指针和动态内存添加新的新结构学生

c++ - 将 char * 转换为 int 会产生奇怪的错误

python - 使用 python 扩展 C++ 应用程序

c++ - std::numeric_limits<int>::has_infinity + 条件三元运算符

c# - 值类型和引用类型只是 C# 概念?

python - 尝试使用另一个 stackoverflow 答案中的矢量角度公式,但无法让它打印角度

php - 使用模具不受欢迎/不好吗?

python - 大型 python tornado 项目的最佳结构是什么?

c++ - 迭代 vector 时越界

c++ - opencv::Mat 来自 vector 的 vector