簡體   English   中英

C ++ .txt值轉換為2D數組

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

我是計算機科學專業的第一學期,上周我得到了這份作業,但我不知道該怎么做。 我們幾乎沒有上課時檢查文件,所以我不確定該如何嘗試。 我不是要為我完成全部任務,只是無法弄清楚如何將給定的txt文件放入2D數組中。 從那里我相信我能弄清楚。

基本上,我們要獲取此.txt文件,並獲取天氣數據並將其放入數組中。 我不確定如何對文件執行此操作。 老實說,我被困住了,而且我在網上找不到任何對我有意義的東西,因為我們在課堂上幾乎沒有涉及到這一點。

先感謝您! :d

作業說明

這是我們得到的天氣文件(.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

這是我進行這項作業的嘗試:

 // 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


*/

}

看這個簡單的例子。 它正在讀取字符串表中的文件。 第一行(帶有表標題)將被忽略。

#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