0

I have been working on this project and been reading in text files with ONLY spaces in between and not commas so I need to update how I read in the file into arrays. I have tried using stringstream() and getline() but no luck. Does anyone know how I can read in this file into arrays?

This is how I been doing it before

void readData(ifstream& inputFile, double lat[], double lon[], double yaw[], int& numLines)
{
// Read in headers.
string header;
getline(inputFile, header);

// Read in data and store in arrays.
for (int i = 0; i < numLines; i++)
{
    if (inputFile >> lat[i])
    {
        inputFile >> lon[i];
        inputFile >> yaw[i];
    }
}

but Im not sure how to modify it to read in this type of file into arrays and also the only ones I'm interested in are

latitude

longitude

altitude(feet)

speed(mph)

gps

power

pitch

roll

yaw

motor on

834,,,,,,,,,,,,,,,,
latitude,longitude,altitude(feet),ascent(feet),speed(mph),distance(feet),max_altitude(feet),max_ascent(feet),max_speed(mph),max_distance(feet),time(millisecond),gps,power,pitch,roll,yaw,motor on
43.5803481,-116.7406331,0,0,0,0,0,0,0,0,539,10,97,178,180,141,0
43.5803481,-116.7406329,0,0,0,0,0,0,0,0,841,10,97,178,180,141,0
43.5803482,-116.7406328,0,0,0,0,0,0,0,0,1125,10,97,178,180,141,0
43.5803481,-116.7406329,-1,0,0,0,0,0,0,0,1420,10,97,178,180,141,0
43.580348,-116.7406328,0,0,0,0,0,0,0,0,1720,10,97,178,180,140,0
43.5803479,-116.7406326,-1,0,0,0,0,0,0,0,2023,10,97,178,180,140,0
43.5803478,-116.7406326,0,0,0,0,0,0,0,0,2344,10,97,178,180,140,0
43.5803476,-116.7406329,-1,0,0,0,0,0,0,0,2620,10,97,178,180,140,0
43.5803475,-116.7406329,-1,0,0,0,0,0,0,0,2922,10,97,178,180,140,0
43.5803473,-116.7406329,0,0,0,0,0,0,0,0,3221,10,97,178,180,140,0

If someone can point in the right direction that would be great. Thanks

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
iAmTheDank
  • 11
  • 2
  • I look at the other question and it kinda helped me but I still don't know how to exclude some of the data since I only need latitude, longitude, altitude(feet), speed(mph), gps, power, pitch, roll, yaw, and motor on – iAmTheDank Apr 18 '15 at 23:56
  • @iAmTheDank :You can use String.split(',') to split up the incoming information. HOWEVER, if you happen to have "I,John Carpenter" or some other string with a comma in it - then it will split it up wrong. Your data doesn't so you should be safe. This will get everything into an array for you. http://howtodoinjava.com/2014/06/02/4-ways-to-splittokenize-strings-in-java/. So you just keep what you want an throw the rest away (or just store everything into an array). – Mark Manning Jan 22 '16 at 01:28

1 Answers1

0

You need to extract those commas, to the variable of type char:

std::ifstream f("d:\\temp\\z.txt");
string s;
double lat, lon, alt, asc, speed, dist, max;
vector<double> lat_vec, lon_vec, asc_vec, speed_vec, dist_vec, max_vec;
char c;
while (!f.eof()) {
    f>>s; // get one line
    stringstream st(s);
    // below, eat first number, then comma, then second number, etc.
    if (st>>lat>>c>>lon>>c>>alt>>c>>asc>>c>>speed>>c>>dist>>c>>max) {
        lat_vec.push_back(lat); lon_vec.push_back(lon);
        asc_vec.push_back(asc); speed_vec.push_back(speed);
        dist_vec.push_back(dist); max_vec.push_back(max);
    }
}
// if you really need arrays, not vectors
double *dist_ar = new double[dist_vec.size];
for (int i=0;i<dist_vec.size(); i++)
    dist_ar[i]=dist_vec[i];
return 0;
scoutcat
  • 29
  • 4
  • Thanks for the post however, I need them to go into arrays – iAmTheDank Apr 19 '15 at 00:00
  • how your arrays are arranged? do you have one array for latitude, one for longitude, etc, or something else? In any case, if you have one-dimensional array, you can do st>>ar[0]>>c>>ar[1]>>c ... etc. If you don't know dimensions, you could use vector instead and push_back values as you read them. – scoutcat Apr 19 '15 at 00:03
  • yes one array for each so I would have a total of 10 – iAmTheDank Apr 19 '15 at 00:04
  • see new version above – scoutcat Apr 19 '15 at 00:12
  • Thanks! The only issue now is how do I read in the first line that has multiple commas? Your example uses f>>s and stingstream but when I output s, I still see the commas – iAmTheDank Apr 21 '15 at 05:37
  • this example will ignore first line, because it won't be able to get any numbers (under if). – scoutcat Apr 22 '15 at 06:17