将文件中的每列排序为单独的数组

时间:2014-04-07 19:10:43

标签: c++ arrays

所以我有一个包含六列的文件,第一列是日期,其余的是不同种类的库存信息。 (即打开,关闭,交易量等)我试图将每列排序成一个数组。这是我的职责:

void load_file(istream& fin, string date[], double open[], double high[],
               double low[], double close[], int vol[], int& day)
{
    day = 0;
    while (day < MAX_TRADING_DAYS)
    {
        fin >> date[day] >> open[day] >> high[day]
            >> low[day] >> close[day] >> vol[day];
        day++;
    }
}

以下是该文件的典型行(总共262行,对应max_trading_days):

3-Mar-14    1206.75 1207.84 1192.14 1202.69 2108720

当我将股票信息的分析输出到另一个文件中时,我完全得到了垃圾值,因此我猜测问题是如何将信息看到数据库中第一名。什么是实现这一目标的最佳方式?

当我尝试显示其中一个数组时,XCode会显示几个不同的

此外,该文件最初在每个列的标题处都有一个标题行。我删除了这个以试图弄清楚这段代码是否有效。从文件中获取信息时如何忽略第一行?

谢谢!

编辑:当我在调用函数后尝试显示其中一个数组时,XCode会显示几个不同的问题,我不确定如何导航。

_LIBCPP_INLINE_VISIBILITY
bool __is_long() const _NOEXCEPT
    {return bool(__r_.first().__s.__size_ & __short_mask);}

_LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
    {return __is_long() ? __get_long_size() : __get_short_size();}

其中每一个都显示&#34;线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x7fff5fcbdd78)&#34;

2 个答案:

答案 0 :(得分:0)

您正在修改参数的副本

在C ++中,参数按值传递。这意味着原始变量中的内容副本将传递给该函数。因此,如果您修改参数,则表示您正在修改副本,而不是原始副本。

您可以通过两种方式解决这个问题:1)通过引用传递或2)通过指针传递。第一种方法允许您修改或访问原始变量而无需复制。第二种方法允许您更改原始变量的内容,因为您知道它的位置。指针很讨厌,更喜欢参考。

因此,编译器可能会抱怨您正在写入变量的副本,或者变量是作为只读变量传递的。

编辑1:简化建议
您可以通过创建结构或类来模拟文本文件中的行来简化函数签名。通过引用将实例传递给输入函数,或者通过引用传递std::vector以输入多行。

此外,您应该在结构中重载operator>>以简化输入循环:

struct Data_Row  
{
  friend std::istream& operator>>(std::istream& input, Data_Row& d);
};

std::istream& operator>>(std::istream& input, Data_Row& d)
{
  // read members of 'd' from 'input'
  return input;
}

//.. your input loop
Data_Row dr;
std::vector<Data_Row> container;
while (input_file >> dr)
{
  container.push_back(dr);
}

答案 1 :(得分:0)

如果代码中存在问题,则可能在其他地方(例如,如何分配内存)。下面是一个使用您的代码的工作示例,它以您指定的格式正确读取输入。

#include <iostream>
#include <fstream>
#include <string>
#include <limits>

#define MAX_TRADING_DAYS 262

using namespace std;

void load_file(istream& fin, string date[], double open[], double high[],
        double low[], double close[], int vol[], int& day)
{
    // Ignore the header row
    fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // Start reading
    day = 0;
    while (day < MAX_TRADING_DAYS)
    {
        fin >> date[day] >> open[day] >> high[day]
            >> low[day] >> close[day] >> vol[day];
        day++;
        if (fin.eof()) break;
    }

    // We will not detect EOF until an extra line has been read, so we compensate.
    day--;
}

// A small function verify that we read correctly.
void dump_file(string* date, double* open, double* high,
        double* low, double* close, int* vol, int& day) {
    for (int i = 0; i < day; i++)
        cout << date[i] << "\t" 
            << open[i] << "\t"
            << high[i] << "\t"
            << low[i] <<  "\t"
            << close[i] <<  "\t"
            << vol[i] << endl;
}


int main(){
    ifstream mystream("Trade.in", std::ifstream::in);

    string* date     = new string[MAX_TRADING_DAYS];
    double* open     = new double[MAX_TRADING_DAYS];
    double* high     = new double[MAX_TRADING_DAYS];
    double* low      = new double[MAX_TRADING_DAYS];
    double* close    = new double[MAX_TRADING_DAYS];
    int* vol         = new int[MAX_TRADING_DAYS];
    int day;

    load_file(mystream, date,open,high,low,close,vol,day); 
    dump_file(date,open,high,low,close,vol,day); 
}
相关问题