C++ 从fstream中的文本一直读到某一行

C++ 从fstream中的文本一直读到某一行,c++,fstream,C++,Fstream,这是我在stackoverflow中的第一个问题。我目前正在做一项作业,只使用字符串、向量和fstream读取文本文件中的总体统计数据 #include <iostream> #include <vector> #include <fstream> using namespace std; struct weather{ string year,month,sky,mintemp,maxtemp,totrain,y, date; int r

这是我在stackoverflow中的第一个问题。我目前正在做一项作业,只使用字符串、向量和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

输出


当然,有一种方法可以做到这一点,而无需使用
goto
。我想不出还有什么情况需要
goto

查看遍历向量的过程。一个好的参考是

其中的代码段示例为:

#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;
}
根据您在下面输入的注释,您的代码是正确的-您已经读取了整个文件,并且有可用的数据来操作它。你们现在想要的是追踪年份、降雨和partlyCloudy。最简单的方法是创建另一个结构,并将其存储在向量中。结构应该是这样的:

struct yeardata {
    string year; // could also be int, if you want to convert it to an ant
    int numberOfRain;
    int numberOfPartlyCloudy;
}
然后,当您在
vw
中迭代时(或者,当您读取文件时,您仍然不需要
vw
向量),您将得到一个yeardata向量。检查向量以查看年份是否存在,如果没有添加,则将两个
int
s设置为0。然后,根据需要增加整数,并打印出该向量中的所有数据


因为这是一项任务,所以我只能带你走这么远了。祝你好运

当然有一种方法可以做到这一点,而不必使用
goto
。我想不出还有什么情况需要
goto

查看遍历向量的过程。一个好的参考是

其中的代码段示例为:

#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;
}
根据您在下面输入的注释,您的代码是正确的-您已经读取了整个文件,并且有可用的数据来操作它。你们现在想要的是追踪年份、降雨和partlyCloudy。最简单的方法是创建另一个结构,并将其存储在向量中。结构应该是这样的:

struct yeardata {
    string year; // could also be int, if you want to convert it to an ant
    int numberOfRain;
    int numberOfPartlyCloudy;
}
然后,当您在
vw
中迭代时(或者,当您读取文件时,您仍然不需要
vw
向量),您将得到一个yeardata向量。检查向量以查看年份是否存在,如果没有添加,则将两个
int
s设置为0。然后,根据需要增加整数,并打印出该向量中的所有数据



因为这是一项任务,所以我只能带你走这么远了。祝你好运

我发布的是正确的吗?你能从weather.txt发布格式或样本内容吗?当然。2012-01-01 Rain 7 13 0.28 2012-01-02散射云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如果看起来很凌乱,我很抱歉,我显然不知道如何发布这篇文章正确吗?你能从weather.txt发布格式或示例内容吗?当然。2012-01-01 Rain 7 13 0.28 2012-01-02散射云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如果它看起来很凌乱,我很抱歉,我显然不知道如何张贴它,但这意味着它只读取特定年份的数据,当我到达下一年时,它停止读取,只打印出它读取的年份,是吗?我发布的第二个代码片段将只输出您关心的一年的数据。如果您需要对数据执行其他操作,请像您一直在做的那样将其存储在向量中,并以第一个代码段为例对向量进行迭代。我明白您的意思。但是你的第二个代码片段是仅为1974年读取和打印的,对吗?但是如果在文本中有另一组年份和不同的数据要打印出来呢……我应该应用!f_stream.eof()以便它能够在下一年打印?如果是,我如何将当前年度增加到下一年度?我是否将年份转换为int数据类型并将其递增,然后将其改回string?我的第二个代码段将打印出一年的数据。如果你想在后面读一个,读入一个向量并使用迭代器是最有意义的
f_stream.eof
检查文件是否完成。在这种情况下,它不太可能帮助你。你的问题让我很困惑——你到底想让你的代码完成什么?你的投入是什么,您的预期输出是什么?示例文本文件:
2012-01-01 Rain 7130.28
2012-01-02散射云彩480.25
2012-01-03 Rain 6120.28
2012-01-04 Rain 5100.28
2013-01-01 Rain 3800.28
2013-01-02 Rain 2110.25
PartlyCloudy 9 11 0.28
2013-01-04 PartlyCloudy 8 10 0.28
以下是示例输出:`year:2012``rain=(当年发生次数)`
fog=“
year:2013
rain=(今年发生次数)
fog=“”
但这意味着它只读取该特定年份的数据,当我到达下一年时,它停止读取,只打印出它读取的年份,是吗?我发布的第二个代码片段将只输出您关心的一年的数据。如果您需要对数据执行其他操作,请像您一直在做的那样将其存储在向量中,并以第一个代码段为例对向量进行迭代。我明白您的意思。但是你的第二个代码片段是仅为1974年读取和打印的,对吗?但是如果在文本中有另一组年份和不同的数据要打印出来呢……我应该应用!f_stream.eof()以便它能够在下一年打印?如果是,我如何将当前年度增加到下一年度?我是否将年份转换为int数据类型并将其递增,然后将其更改回int
struct yeardata {
    string year; // could also be int, if you want to convert it to an ant
    int numberOfRain;
    int numberOfPartlyCloudy;
}