【问题标题】:reading file columns into an array将文件列读入数组
【发布时间】:2015-11-11 03:38:20
【问题描述】:

我是编程新手,所以我有一个可能是基本问题。我目前有一个包含 365 行的文本文件……一年中每天一行。这些是文件的前四行:

2003 1 1 18 0 -1 36 50 46
2003 1 2 16 3 -1 43 56 52
2003 1 3 19 7 -1 42 56 49
2003 1 4 14 3 -1 42 58 50

我最终必须使用提供给我们的特殊库来绘制这些图表,但首先我想将每一列的数据放入一个数组中。这是我尝试这样做的代码的一部分。

#include "library.h"
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

ifstream in;
int yr[364], mo[364], day[364], windSpeed[364], precip[364], snowDepth[364], minTemp[364], maxTemp[364], avgTemp[364];

void main() {
    make_window(800, 800);
    set_pen_color(color::red);
    set_pen_width(8);

// open file, read in data
    in.open("PORTLAND-OR.TXT");
    if (in.is_open()) {
        // read each column into an array
        for (int i = 0; i < 364; i++) {
            in >> yr[i] >> mo[i] >> day[i] >> windSpeed[i] >> precip[i] >> snowDepth[i] >> minTemp[i] >> maxTemp[i] >> avgTemp[i];
            cout << mo[i] << "    " << day[i] << endl;

        }
        in.close();
    }
    else {
        cout << "error reading file" << endl;
        exit(1);
    }
}

当我尝试打印第二列和第三列(月和日)中的所有值时,它会从 3 月 8 日 (3 8) 到 12 月 31 日 (12 31) 开始打印。我需要它从 1 月 1 日到 12 月 31 日一直打印。前两个月的价值没有打印有什么原因吗?

【问题讨论】:

  • 如果不查看数组的声明方式和打印方式,就很难看出问题所在。
  • 贴出更多有用的代码如数组声明,,,
  • 需要输入文件,因为我已经用很少的输入进行了测试并且它正在工作
  • 我粘贴了输入文件的前四行。该文件包含 365 行。第一列是年份,第二列是月份,第三列是日期。其余列是与天气有关的数据。

标签: c++ loops


【解决方案1】:

下面是一个非常愚蠢的仅主程序,它使用您现有的阅读代码读入您的文件并再次将其打印出来。我在代码中嵌入了一个运行注释,其中包含您应该考虑做的事情。

基本上,这就是我昨天在loop not displaying all data entries from file谈论的内容

#include <iostream>
#include <fstream>

using namespace std; // bad! Avoid doing this in real life.

int yr[364], 
    mo[364], 
    day[364], 
    windSpeed[364], 
    precip[364], 
    snowDepth[364], 
    minTemp[364], 
    maxTemp[364], 
    avgTemp[364]; // bad! define a structure instead


/* Example:
struct stats
{
    int yr; 
    int mo; 
    int day; 
    int windSpeed; 
    int precip; 
    int snowDepth; 
    int minTemp; 
    int maxTemp; 
    int avgTemp;
};

struct stats statistics[364]; // bad! use a std::vector instead

std::vector<stats> statistics; 
*/

int main()
{
    // removed all the windowing stuff.
    ifstream in;
    in.open("PORTLAND-OR.TXT");
    if (in.is_open())
    {
        // read each column into an array
        for (int i = 0; i < 364; i++)
        { // what do you do if the file ends early or contains bad information?
            in >> yr[i] >>
                  mo[i] >>
                  day[i] >>
                  windSpeed[i] >>
                  precip[i] >>
                  snowDepth[i] >>
                  minTemp[i] >>
                  maxTemp[i] >>
                  avgTemp[i];
        }
        in.close();
    }
    else
    {
        cout << "error reading file" << endl;
        return 1;
    }
    // printing out all the stuff that was read 
    for (int i = 0; i < 364; i++)
    {
        cout << yr[i] << "," <<
                mo[i] << "," <<
                day[i] << "," <<
                windSpeed[i] <<  "," <<
                precip[i] <<  "," <<
                snowDepth[i] <<  "," <<
                minTemp[i] <<  "," <<
                maxTemp[i] <<  "," <<
                avgTemp[i] << endl;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多