如何在没有数组的情况下从文件中读取不同大小行的变量

How to read in variables from different size lines from file without an array

本文关键字:读取 变量 数组 情况下 文件      更新时间:2023-10-16

我刚开始编程,不知道很多,但基本上我必须写这个程序:

   Write a program called WeatherStats which will read a RAWS weather file and will return the following statistics:
   Range of dates in the file
   Average high temperature
   Average low temperature
   Maximum high temperature (include date)
   Minimum low temperature (include date)
   Total rainfall
   How many days it rained

我知道如何读取文件并分配我想要的变量,但行不一样,这就是我目前所拥有的:

    if (weatherStats.is_open())
{
        do
        {
            weatherStats >> month >> day >> precip >> hour1 >> hour2 >> temp1 >> temp2 >> hum1 >> hum2 >> elevation >> precipdur1 >> precipdur2;
            cout << month << " " << day << " " << precip << " " << hour1 << " " << hour2 << " " << temp1 << " " << temp2 << " " << hum1 << " " << hum2 << " " << elevation << " " << precipdur1 << " " << precipdur2;
            cout << endl;
        } while (!weatherStats.eof());
    weatherStats.close();
}
system("PAUSE");

}

我遇到的问题是,不是文件中的每一行都有相同数量的数字,所以当我从文件中将它们读入变量时,我在第一行之后得到了错误的数字。

我一直在为此而挣扎,如果有任何帮助,我将不胜感激。

这是文件:

格式:

1. Month 
2. Day 
3. Precipitation – the daily rain amount specified in hundredths of an inch or     millimeters (integer) 
4. Hour 1 – the hour at which the minimum temperature was recorded (0-2400) 
5. Hour 2 – the hour at which the maximum temperature was recorded (0-2400) 
6. Temperature 1 – minimum temperature in degrees Fahrenheit or Celsius (integer) 
7. Temperature 2 – maximum temperature in degrees Fahrenheit or Celsius (integer) 
8. Humidity 1 – maximum humidity in percent, 0 to 99 (integer) 
9. Humidity 2 – minimum humidity in percent, 0 to 99 (integer) 
10. Elevation - feet or meters above sea level 
11. Precipitation Duration (optional) -  the beginning and ending times (0-2400)

7 1 16 2300 1400 50 80 99 26 4570 1700 2000
7 2 0 500 1600 46 84 99 24 4570
7 3 11 400 1500 50 88 99 24 4570 1700 1800
7 4 0 600 1600 54 85 63 28 4570
7 5 0 500 1600 50 76 86 31 4570
7 6 0 500 1600 44 82 83 23 4570
7 7 0 500 1500 43 83 76 14 4570
7 8 0 500 1800 42 84 67 18 4570
7 9 0 500 1600 43 88 69 12 4570
7 10 0 400 1600 46 87 59 14 4570
7 11 0 600 1600 43 76 29 8 4570
7 12 0 400 1700 36 84 51 10 4570
7 13 0 500 1600 39 87 45 10 4570
7 14 0 600 1700 42 86 53 8 4570
7 15 0 200 1500 51 83 44 18 4570
7 16 10 400 1300 50 81 58 22 4570 1100 1200
7 17 0 600 1700 44 84 82 15 4570
7 18 1 500 1200 46 83 77 19 4570 500 600
7 19 0 500 1700 41 87 76 11 4570

您可以使用stringstream

  • 首先在所有int变量中存储默认值

  • 然后从文件流中读取一行到字符串变量

  • 然后通过stringstream将该字符串放入所有int变量中。

以下是示例代码:(根据您的需要修改)

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main (){
  //....
  //....
  if (weatherStats.is_open()){
      string str="";
      do{
          month=0;day=0;precip=0;hour1=0;hour2=0;temp1=0;temp2=0;hum1=0;
          hum2=0;elevation=0;precipdur1=0;precipdur2=0;
          //put default values in all variable in each loop
          //NOTE: as I can see in your input file only last two is missing, SO you can here set default values to last two variable only, not for all the variables
          str=weatherStats.ReadLine();
          //read line in to string
          // then put all those in your all variables
          stringstream(str) >> month >> day >> precip >> hour1 >> hour2 >> temp1 >> temp2 >> hum1 >> hum2 >> elevation >> precipdur1 >> precipdur2;
          cout << month << " " << day << " " << precip << " " << hour1 << " " << hour2 << " " << temp1 << " " << temp2 << " " << hum1 << " " << hum2 << " " << elevation << " " << precipdur1 << " " << precipdur2;
          cout << endl;
      } while (!weatherStats.eof());
      weatherStats.close();
  }
  //.......
}

请查看本页的最后部分-字符串流以获取帮助。

这里,若字符串包含较少的数字,那个么额外的变量将不会改变,所以它们的默认值将保持不变。