Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

populate a struct from external file

Posted on 2004-03-25
4
Medium Priority
?
249 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 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.
Suggested Courses

670 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