0

Hi there,

ok, im fairly new to c++ and have been trying to figure this out for ages:

I have a text file i want to read in that contains lines and lines of lat long points;
N50 42.22 W002 55.33
N50 42.22 W002 55.33
N50 42.22 W002 55.33

i want to seperate each column and data type and out that into an array (colunms are seperated by a tab "\t"), so
String s[] = {N,N,N}
int x[] = {50,50,50}
double y[] = {42.22,42.22,42.22}
etc

So far i can read in each line:

string line;
  ifstream myfile ("lat_longs.txt");
  myfile.is_open();
  
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();

But i don't know how to loop over each character, check its data type and store it in the appropriate array? hit a "\n" and do it all again until eof.

I feel like i'm barking up the wrong tree with the above and that i'm missing something, is there a function that already does this?

Anyone? HELP!

Many Thanks

Alex

Edited by Alexkid: n/a

3
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

Instead of having all those arrays it would be better if you created a structure or class to contain all the data for one line, then create a vector of those classes

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

struct item
{
   char s;
   int x;
   double y;
};
int main()
{

    std::vector<item> items;
    std::string line;
    std::ifstream myfile("textfile.txt");
    while( std::getline(myfile, line) )
    {
        item it;
        std::stringstream str;
        str << line;
        str >> it.s >> it.x >> it.y;
        items.push_back(it);
    }
}
This question has already been answered. 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.