gpt4 book ai didi

C++ .txt 值到二维数组

转载 作者:行者123 更新时间:2023-11-28 04:28:44 26 4
gpt4 key购买 nike

我是计算机科学专业第一学期的学生,上周我接到了这个作业,但我不知道该怎么做。我们几乎没有在类里面复习过文件,所以我什至不确定如何尝试这个。我并不是要求为我完成整个作业,我只是不知道如何将给定的 txt 文件放入二维数组中。从那里我相信我可以弄清楚。

基本上,我们正在获取此 .txt 文件并获取天气数据并将其放入一个数组中。我不确定如何使用文件执行此操作。老实说,我被困住了,我没有在网上找到任何对我有意义的东西,因为我们在类里面几乎没有讲过这个。

提前致谢! :D

Assignment Description

这是我们得到的天气文件 (.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
}
}

关于C++ .txt 值到二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53554082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com