Solved

populate a struct from external file

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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