Solved

populate a struct from external file

Posted on 2004-03-25
4
229 Views
Last Modified: 2010-04-01
If I receive data from an  external file in the following format:

HK0.24690HONG KONG
HM1.11105HEARD & MCDONALD ISLAND
HN0.12345HONDURAS

how can I populate a struct similar to:

struct Entry
{
  string abbrev;
  string country;
    float rate;
};

the struct would be in a vector
0
Comment
Question by:Stella Pauley
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10682898
You need to parse the file (line by line) and extract the information you need for your struct. From the example, it looks like the first part is either two characters, or a number of characters which are all capital letters. Only you know what the exact format is... Then, you have a floating point number that consists of digits and a decimal separator, followed by a string that goes to the end of the line.

Do you know how to extract this data?

Once you have the data, you do something like this:

vector<Entry> theEntryVector;

...

    while(not_end_of_file)
    {
        Entry newEntry;
        newEntry.abbrev = abbrev;
        newEntry.country = country;
        newEntry.rate = rate;

        theEntryVector.push_back(newEntry);
    }

This assumes that you've extracted the three parts into the variables abbrev, country and rate.

0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
ID: 10683083
Here is a sample program that shows the mechanism:

#include <vector>
#include <string>
#include <fstream>
#include <iostream>

#include <stdlib.h>

using namespace std;

struct Entry
{
    string abbrev;
    string country;
    float rate;
};



int main()
{
    vector<Entry> theEntryVector;

    ifstream is;
    is.open("country.txt");

    string line;

    while (getline(is, line))
    {
        Entry newEntry;

        // extract the first two characters:
        newEntry.abbrev = line.substr(0,2);

        // extract the exchange rate
        int index = 2;  // start with the first character after the abbrev:
        while (isdigit(line[index]) || line[index] == '.')
        {
                index++;
        }

        // extract the country name
        string rateString = line.substr(2, index-2);
        newEntry.rate = atof(rateString.c_str());
        newEntry.country = line.substr(index, line.size() - index);
    }


    return 0;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 10683967
To get the number, you don't need to worry about the end position of the number.
So you really don't need to use a temporary rateString variable.

The following would do the trick
newEntry.rate = atof(line.substr(2).c_str());

If you're file has fix length fields, then you can read the data without having to search for the end of the number, which would make your code much more efficient.

Example:
   while (getline(is, line))
   {
       Entry newEntry;

       newEntry.abbrev = line.substr(0,2);
       newEntry.rate = atof(line.substr(2).c_str());
       newEntry.country = line.substr(9);
   }
0
 
LVL 30

Expert Comment

by:Axter
ID: 10683975
I forgot to add that the while loop is missing the part in which it actually adds the data to the vector.
   while (getline(is, line))
   {
       Entry newEntry;

       newEntry.abbrev = line.substr(0,2);
       newEntry.rate = atof(line.substr(2).c_str());
       newEntry.country = line.substr(9);
      theEntryVector.push_back(newEntry);
   }
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now