简体   繁体   English

C ++ .txt值转换为2D数组

[英]C++ .txt values to 2D array

I'm a first semester Computer Science major and I got this assignment last week and I can't figure out how to do it. 我是计算机科学专业的第一学期,上周我得到了这份作业,但我不知道该怎么做。 We barely went over files in class so I'm not really sure how to even attempt this. 我们几乎没有上课时检查文件,所以我不确定该如何尝试。 I'm not asking for my whole assignment to be done for me, I just can't figure out how to put the given txt file into a 2D array. 我不是要为我完成全部任务,只是无法弄清楚如何将给定的txt文件放入2D数组中。 From there I believe I can figure it out. 从那里我相信我能弄清楚。

Basically we are taking this .txt file and taking the weather data and putting it into an array. 基本上,我们要获取此.txt文件,并获取天气数据并将其放入数组中。 I'm not sure how to do this with files. 我不确定如何对文件执行此操作。 I'm honestly stuck and I haven't found anything online that makes sense to me because we've barely gone over this in class. 老实说,我被困住了,而且我在网上找不到任何对我有意义的东西,因为我们在课堂上几乎没有涉及到这一点。

Thank you in advance! 先感谢您! :D :d

Assignment Description 作业说明

Here is the weather file (.txt) we were given: 这是我们得到的天气文件(.txt):

Day High(F) Low(F)  Precip.(inch)   Snow(inch)  SnowDepth(inch)
1 jan 2016  23.0    10.0    0.00    0.00    9.02
2 jan 2016  24.1    -7.1    0.00    0.00    9.02
3 jan 2016  30.0    10.9    0.00    0.00    9.02
4 jan 2016  37.9    28.9    0.01    0.00    7.99
5 jan 2016  41.0    30.9    0.00    0.00    5.98
6 jan 2016  43.0    34.0    0.00    0.00    5.00
7 jan 2016  37.0    28.0    0.00    0.00    5.00
8 jan 2016  35.1    28.9    0.01    0.00    4.02
9 jan 2016  39.0    28.0    0.00    0.00    4.02
10 jan 2016 30.9    23.0    0.00    0.00    4.02
11 jan 2016 30.9    18.0    0.00    0.00    4.02
12 jan 2016 36.0    28.9    0.00    0.00    4.02
13 jan 2016 37.9    32.0    1.30    0.00    2.99
14 jan 2016 39.0    25.0    0.17    0.00    2.99
15 jan 2016 34.0    19.9    0.01    0.00    2.01
16 jan 2016 34.0    26.1    0.24    2.01    2.99
17 jan 2016 37.9    28.9    0.10    0.00    2.99
18 jan 2016 39.9    32.0    0.39    0.00    2.01
19 jan 2016 39.9    30.0    0.03    0.00    2.01
20 jan 2016 41.0    32.0    0.50    0.00    0.98
21 jan 2016 39.0    30.9    0.23    0.00    0.00
22 jan 2016 45.0    35.1    0.13    0.00    0.00
23 jan 2016 44.1    32.0    0.23    0.00    0.00
24 jan 2016 44.1    34.0    0.09    0.00    0.00
25 jan 2016 39.9    28.0    0.00    0.00    0.00
26 jan 2016 39.0    30.0    0.01    0.00    0.00
27 jan 2016 44.1    36.0    0.05    0.00    0.00
28 jan 2016 39.9    36.0    0.67    0.00    0.00
29 jan 2016 39.9    32.0    0.32    0.00    0.00
30 jan 2016 39.0    30.9    0.02    0.00    0.00
31 jan 2016 37.9    28.9    0.01    0.00    0.00

Here are my attempts at this assignment: 这是我进行这项作业的尝试:

 // This program reads data from a file into an array

#include <iostream>
#include <fstream> // To use ifstream
using namespace std;

int main()
{

// My first attempt with the assignment hints

/*
    int x;
    ifstream infile; // Create an ifstream object

        infile.open("weather.txt"); // Open weather file
        infile >> x; // read in a single integer from the file

        cout << x << "\n" << endl; // Test x variable

        int i;
    int array1[x];
        int array2[x];
        int array3[x];

        for(int i = 0; i < 31; i++){
                infile >> array1[i];
                infile >> array2[i];
                infile >> array3[i];
                }

        cout << array1[6];
*/


// Attempt 2:

/*
int table[rows][columns];

        for(int i = 0; i < rows; i++)
        {
                for(int j = 0; j < columns; j++)
                {
                        table[i][j] = 0;

                } // End colums loop

        } // End rows loop


*/

}

Look at this simple example. 看这个简单的例子。 It's reading your file in table of strings. 它正在读取字符串表中的文件。 The first line (with table headers) is ignored. 第一行(带有表标题)将被忽略。

#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;



int main(int argc, char* argv[])
{
    using line = vector<string>; // one line of strings
    using table = vector<line>;  // table is array of lines

    ifstream file("1.txt");
    string s;
    std::getline(file, s); // get header line

    table tbl;

    while(std::getline(file, s)) // getting new line
    {
        stringstream ss(s);
        line ln;
        while(ss >> s)           // parse elements of line
            ln.push_back(s);
        tbl.push_back(ln);       // insert line into table
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM