[英]read until a certain line from text in fstream
This is is my first question in stackoverflow. 这是我在stackoverflow中的第一个问题。 I am currently doing an assignment in reading an overall stats in a text file by only using string and vectors and fstream. 我目前正在通过仅使用字符串和向量以及fstream读取文本文件中的总体统计信息来进行分配。
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct weather{
string year,month,sky,mintemp,maxtemp,totrain,y, date;
int rain = 0,scatter = 0,partly = 0,fog = 0,clean = 0,snow = 0, most = 0,storm = 0,cast = 0;
};
int main(){
string year;
int entry = 0;
vector<weather>vw;
weather w;
ifstream w_file;
w_file.open("weather.txt");
while (w_file >> w.date >> w.sky >> w.mintemp >> w.maxtemp >> w.totrain){
year = w.date.substr(0,4);
cout << year << endl;
}
cout << entry ; //this was just to confirm how many line i have in the file
what I manage to print out was the years of the file. 我设法打印的是文件的年份。 What i wanted to do is to read the data in a specific year and print out the particular year with its contents. 我想做的是读取特定年份的数据并打印出特定年份及其内容。 is there anyway I can do this without using goto? 我是否可以不使用goto来做到这一点?
2012-01-01 Rain 7 13 0.28 2012-01-02 ScatteredClouds 4 8 0.25 2012-01-03 Rain 6 12 0.28 2012-01-04 Rain 5 10 0.28 2012-01-05 Rain 7 12 0.28 2012-01-06 PartlyCloudy 3 9 0.28 2012-01-07 PartlyCloudy 7 11 0.25 2012-01-08 Rain 7 10 0.28 2012-01-09 PartlyCloudy 6 12 0.25 2013-01-01 Rain 3 8 0.28 2013-01-02 Rain 2 11 0.25 2013-01-03 PartlyCloudy 9 11 0.28 2013-01-04 PartlyCloudy 8 10 0.28
year = 2012 rain = 0//rain++ if rain is found partlyCloudy = 0//partly++ if partlyCloudy is found year = 2013 rain = 0//rain++ if rain is found partlyCloudy = 0//partly++ if partlyCloudy is found
Of course there's a way to do this without using goto
. 当然,有一种无需使用goto
即可执行此操作的方法。 I can't think of any case where goto
is required anymore. 我goto
来需要goto
的任何情况。
Look into iterating through your vector. 研究遍历您的向量。 A good reference is http://www.cplusplus.com/reference/vector/vector/begin/ 一个很好的参考是http://www.cplusplus.com/reference/vector/vector/begin/
The code snippet example from there is: 此处的代码段示例为:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
In your case, you'll want to check each *it
for a matching date, and then if the date matches, output the data you want from the appropriate structure. 对于您的情况,您需要检查每个*it
是否有匹配的日期,然后如果日期匹配,则从适当的结构中输出所需的数据。
Or, depending on how the specific date you're looking for is entered, you can put it in your existing while
loop and not even bother with storing the other data in a vector
. 或者,根据要输入的特定日期的输入方式,可以将其放入现有的while
循环中,而不必理会将其他数据存储在vector
。 In that case, your while
loop may look something like: 在这种情况下,您的while
循环可能类似于:
while (w_file >> w.date >> w.sky >> w.mintemp >> w.maxtemp >> w.totrain){
year = w.date.substr(0,4);
if (year == "1974") { // or whatever the year your looking for is...
cout << w.date << w.sky << w.mintemp << w.maxtemp << w.totrain << endl;
}
Given the comments you've put in below, your code is on the right track - you've read in the entire file, and have the data available to manipulate it. 鉴于您在下面添加的注释,您的代码在正确的轨道上-您已阅读了整个文件,并具有可用于操作它的数据。 What you now want is to track year, rain and partlyCloudy. 您现在想要的是跟踪年,雨和部分多云。 The easiest way to do that is to make another structure, and store it in a vector. 最简单的方法是制作另一个结构,并将其存储在向量中。 The structure would be something like this: 结构如下所示:
struct yeardata {
string year; // could also be int, if you want to convert it to an ant
int numberOfRain;
int numberOfPartlyCloudy;
}
Then, when you iterate through vw
(or, as you read the file. You still don't really need the vw
vector), you'll have a vector of yeardatas. 然后,当您遍历vw
(或者,当您读取文件时。您仍然不需要vw
向量),将有一个yeardatas向量。 Check the vector to see if the year exists, and if it doesn't add it, with the two int
s set to 0. Then, increment the ints as appropriate, and print out all the data from that vector. 检查向量以查看年份是否存在,以及是否不将其与两个int
设置为0的年份相加。然后,适当增加整数,并打印出该向量中的所有数据。
Since it's an assignment, that's pretty much as far as I can take you. 由于这是一项任务,因此我可以带您去做。 Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.