Reading from a file and storing the data into a structure

Pages: 12
Okay, not open weatherdata.csv with a text editor and cut and paste the first 5 to 10 lines inside code in a post.

This file will require more processing than what you are currently doing.

alright this is what I got when I opened it with notepad.
Here are the first 10 lines minus the labels.


EVENT_ID,CZ_NAME_STR,BEGIN_DATE,BEGIN_TIME,EVENT_TYPE,DEATHS_DIRECT,INJURIES_DIRECT,DAMAGE_PROPERTY_NUM,BEGIN_LAT,BEGIN_LON
9991511,MIAMI-DADE CO.,10/18/1955,800,Thunderstorm Wind,0,0,0,25.78,-80.28
9991516,MIAMI-DADE CO.,4/10/1956,1730,Thunderstorm Wind,0,0,0,25.78,-80.28
9991517,MIAMI-DADE CO.,4/10/1956,1730,Thunderstorm Wind,0,0,0,25.78,-80.28
9988286,MIAMI-DADE CO.,3/15/1957,1100,Thunderstorm Wind,0,0,0,25.78,-80.28
9988306,MIAMI-DADE CO.,8/3/1957,1500,Thunderstorm Wind,0,0,0,25.78,-80.28
9988307,MIAMI-DADE CO.,8/4/1957,1400,Thunderstorm Wind,0,0,0,25.78,-80.28
9984941,MIAMI-DADE CO.,8/27/1958,1400,Thunderstorm Wind,0,0,0,25.78,-80.28
9984969,MIAMI-DADE CO.,6/17/1959,2050,Thunderstorm Wind,0,0,0,25.7,-80.28
9986107,MIAMI-DADE CO.,9/26/1960,1400,Thunderstorm Wind,0,0,0,25.78,-80.28
9988349,MIAMI-DADE CO.,6/5/1962,1300,Thunderstorm Wind,0,0,0,25.5,-80.68
Last edited on
Okay to read this file you will first need to read and discard that first line. Then you will be left with the remaining fields. for this I recommend you read an entire line at a time using getline() then use a string stream to process that line. You'll need to use a combination of getline() with the optional third parameter, the delimiter (a comma), and the extraction operator.

you mean like this getline(weather, line, '\n');
oh, I almost forgot, did you read the message I sent you.
Last edited on
couldn't I do this to skip over the first line Jlb?

1
2
3
4
5
6
7
8
9
10
11
12
while(weather >> data.eventID >> data.CZ_NAME >> data.beginDate
       >> data.beginTime >> data.eventType >> data.deaths
       >> data.injuries >> data.propertyDamage >> data.beginLatitude
       >> data.beginLongitude)
    {
      // ignore the first line.
      weather.ignore(numeric_limits<streamsize>::max(), '\n');

      /* Add all the data that was added to each member of the
         structure, into the vector. */
      weatherInformation.push_back(data);
    }
Only if you do it before you read the first line, a simple getline() would be just as easy.

So like I've said before it would be something like this, getline(weather, line, ',')?

weather being the object that opens the file
line being the string stream
and then ',' being the delimiter.

If so, what would I do with the string stream(in this case, the string line).

would I then add the string stream to the vector since the vector is a structure.
Last edited on
Like I said before I recommend reading the complete line from the file with getline() then use a stringstream to for parsing. When parsing the line you will use a combination of getline() with the delimiter, a comma, and the extraction operator. Using getline() for the strings and the extraction operator for the numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
...
struct Weather_Event
{
  int eventID; // is the unique int id associated with the weather event.
  std::string CZ_NAME; // is the location associated with the weather event.
  std::string beginDate; // is the date the event started.
  std::string beginTime; // is the time the event started.
  std::string eventType; // is the type of weather event.
  int deaths; // are the number of people killed by the weather.
  int injuries; // are the number of people injured by the event.
  int propertyDamage; /* is the $ worth of property damage caused
                         by the event. */
  float beginLatitude; // is the starting latitude for the event.
  float beginLongitude; // is the starting longitude for the event.
};
...
int main()
{
...
string line;
char comma;
while(getline(weather, line))
{
   Weather_Event temp;
   istringstream sin(line);
   sin >> temp.eventID >> comma;
   getline(sin, temp.CZ_NAME, ',');
   getline(sin, temp.,beginDate ',');
   getline(sin, temp.beginTime, ',');
   getline(sin, temp.eventType,',');

   sin >> temp.deaths >> comma >> temp.injuries >> comma >> etc.

  if(sin)
     push_back(temp); // 
}
Done, easy enough. Problem solve. Thanks for taking time out of your day to help me understand this.
Topic archived. No new replies allowed.
Pages: 12