0

Ok I need help on this Freshman year project-( so it should involve loops, arrays/strings,fstreams and functions only)

A CSV file contains airport info:

ABZ,"Dyce","Aberdeen","Scotland, United Kingdom","GB",57.200000,-2.200000,493,\N,"Aberdeen",2657832,2638360
EDI,"Turnhouse","Edinburgh","Scotland, United Kingdom","GB",55.950000,-3.350000,493,\N,"Edinburgh",2650225,2638360
BXE,"Bakel","Bakel","Senegal","SN",14.916667,-12.908333,569,"Senegal",\N,\N,2245662
MFV,"Accomack County","Melfa","United States","US",13.633333,39.133333,67,"Verenigde Staten",\N,\N,6252001
ADK,"Adak Island Ns","Adak Island","United States","US",51.878056,-176.646111,67,"Verenigde Staten",\N,\N,6252001
LIT,"Adams Field Airport","Little Rock, AR","United States","US",34.729444,-92.224444,67,"Verenigde Staten",\N,5035199,6252001
ADS,"Addison Airport","Dallas","United States","US",32.968611,-96.836389,67,"Verenigde Staten",\N,5722064,6252001
SLK,"Adirondack","Saranac Lake","United States","US",44.400000,-74.200000,67,"Verenigde Staten",\N,5136322,6252001
INS,"Af Aux","Indian Springs","United States","US",46.283333,22.100000,67,"Verenigde Staten",\N,5506040,6252001
HST,"AFB","Homestead","United States","US",25.483333,-80.483333,67,"Verenigde Staten",\N,5194067,6252001
MUO,"AFB","Mountain Home","United States","US",-32.816667,27.133333,67,"Verenigde Staten",\N,5601615,6252001
OHC,"AFS","Northeast Cape","United States","US",63.316667,-168.966667,67,"Verenigde Staten",\N,\N,6252001
PML,"AFS","Port Moller","United States","US",55.983333,-160.533333,67,"Verenigde Staten",\N,\N,6252001
ANW,"Ainsworth","Ainsworth","United States","US",42.583333,-99.983333,67,"Verenigde Staten",\N,5062769,6252001
HWD,"Air Terminal","Hayward","United States","US",37.666667,-122.083333,67,"Verenigde Staten","Hayward",5355933,6252001

I need a file I/O algo that will extract the :

  1. 3-letter CODE ....into a string/char array
  2. City Name: (Third piece of info/ after the 2nd comma).... into a string/char array
  3. Country Name: comes directly after city name ...into a string/char array
  4. The longitude and latitude values given in the 6th and 7th piece of info. into two different float arrays.

All the rest can be dumped.

Here is what Ive done so far:

#include <fstream>
#include <iostream>
#include "conio.h"
#include <cstring>
using namespace std;



int main()
{
    ifstream readFile("airports.csv");     //open file
    int i=0 , j=0;                         //variables for counters & loop control
    char c;
    // strings to store code, city, country, lat, long values  (1,3,4,6,7)
    
    char codeNum[9000][5]; 
    char city[9000][20];
    char country[9000][40];
    
    double lati[9000];
    double longi[9000];
    
    
    if( readFile.fail( ) )
        {
            cout << "Input file opening failed.\n";
            getch();  return (1);             }             
    
    
    //readFile>>k;
    //cout<<k;    
    
    
    
    
    
    
        while(!readFile.eof( ))
    {
          readFile.get(c);
          //if ( c=='\n')
          //increment to next line;
          //else
          cout << c;
    }
    
    
    
    
    
    
    readFile.close();
     
    
    getch();
    return 0;
}

Edited by Der_sed: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by vmanes
0

Since you have a very formatted data, use getline to read up to the commas, and on the last item of the record use getline with no specified delimeter to get you to the end of line.

You will have read each item, if only into a temporary string which you ignore in the cases of the undesired items. I'd have a specific temporary input string for each item, then only store the ones you're interested in.

Also, for controlling the reading loop, don't use the .eof( ) function. Better to use the result of getline in reading the potential first item of a record, like

while ( getline( readFile, tempCode, ','  ) )
   { 
          //loop to read the rest of record's fields

          //store the desired items
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.