2014-11-21 43 views
0

我是编程新手,所以我可以使用你的帮助与我的代码。我想读取一个数据格式像这样一个现有的文件:我张贴了我所有的代码C++:读取分隔文件并存储在结构中

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 

,但相关功能是无效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); 
} 
+0

请考虑在你的问题是什么更具体一些。你有什么尝试,什么不工作。在我的头顶上,当你的交易通过引用传递时,你的许多例程都会按照价值交易你的交易。否则,您正在修改事务的_copy_,而不是原始事务本身。 – fbrereto 2014-11-21 07:22:35

回答

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 
+0

感谢您的回复。那么,是否没有办法跳过向矢量的推入并直接将值加载到结构中? – 2014-11-21 18:39:15

+0

@JPark当然。但这个例子并不简单。 – Oncaphillis 2014-11-21 19:56:53

相关问题