【问题标题】:read until a certain line from text in fstream从 fstream 中的文本读取直到某一行
【发布时间】:2016-02-17 04:32:31
【问题描述】:

这是我在 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 的情况下做到这一点吗?


数据文件
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 

【问题讨论】:

  • 我发布的正确吗?
  • 你能把weather.txt的格式或示例内容贴出来吗?
  • 当然。 2012-01-01 雨 7 13 0.28 2012-01-02 散云 4 8 0.25 2012-01-03 雨 6 12 0.28 2012-01-04 雨 5 10 0.28 2012-01-05 雨 7 12 0.28 2012-01-06多云 3 9 0.28 2012-01-07 多云 7 11 0.25 2012-01-08 雨 7 10 0.28 2012-01-09 多云 6 12 0.25
  • 很抱歉,如果它看起来很乱,我显然不知道如何发布它

标签: c++ fstream


【解决方案1】:

当然有一种方法可以在不使用goto 的情况下做到这一点。我想不出任何需要goto 的情况。

考虑迭代你的向量。一个很好的参考是http://www.cplusplus.com/reference/vector/vector/begin/

那里的代码 sn-p 示例是:

#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;
}

在您的情况下,您需要检查每个 *it 的匹配日期,然后如果日期匹配,则从适当的结构中输出您想要的数据。

或者,根据您要查找的具体日期的输入方式,您可以将其放入现有的while 循环中,甚至不必费心将其他数据存储在vector 中。在这种情况下,您的 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;
}

鉴于您在下面输入的 cmets,您的代码在正确的轨道上 - 您已经阅读了整个文件,并且拥有可用于操作它的数据。您现在想要的是跟踪年份、下雨和部分多云。最简单的方法是制作另一个结构,并将其存储在向量中。结构是这样的:

struct yeardata {
    string year; // could also be int, if you want to convert it to an ant
    int numberOfRain;
    int numberOfPartlyCloudy;
}

然后,当您遍历vw(或者,当您阅读文件时。您仍然不需要vw 向量),您将拥有一个yeardatas 向量。检查向量是否存在年份,如果不添加,则将两个ints 设置为0。然后,酌情递增整数,并打印出该向量中的所有数据。

因为这是一项任务,所以我可以带您到此为止。祝你好运!

【讨论】:

  • 但这意味着它只读取特定年份,当我到达第二年时它停止读取并且只打印出它读取的年份是吗?
  • 我贴的第二个代码sn-p只会输出你关心的一年的数据。如果你需要对数据做其他事情,就照你一直做的那样把它存储在vector中,并以第一个代码sn-p为例对vector进行迭代。
  • 我明白你的意思。但是您的第二个代码 sn-p 只能在 1974 年读取和打印,对吗?但是,如果在文本中有另一组年份和不同的数据要打印出来……我应该应用 !f_stream.eof() 以便它能够在下一年打印吗?如果是这样,我如何将当前年份增加到下一年?我是否将年份转换为 int 数据类型并将其递增,然后将其更改回字符串?
  • 我的第二个 sn-p 打印出一年的数据。如果你想要之后的那个,读入一个向量并使用迭代器是最有意义的。 f_stream.eof 检查文件是否完成。在这种情况下,它不太可能对您有所帮助。不过,您的问题使我感到困惑-您到底想让代码完成什么?您的输入是什么,您的预期输出是什么?
  • 示例文本文件:2012-01-01 Rain 7 13 0.282012-01-02 ScatteredClouds 4 8 0.252012-01-03 Rain 6 12 0.282012-01-04 Rain 5 10 0.282013-01-01 Rain 3 8 0.282013-01-02 Rain 2 11 0.252013-01-03 PartlyCloudy 9 11 0.282013-01-04 PartlyCloudy 8 10 0.28 以下是示例输出:`年份:2012`雨=(当年发生的次数)`fog = " "year : 2013rain = (number of occurence in this year)fog = " "
猜你喜欢
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 2019-05-06
相关资源
最近更新 更多