Solved

populate a struct from external file

Posted on 2004-03-25
4
235 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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