Solved

populate a struct from external file

Posted on 2004-03-25
4
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

630 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