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

Recommended Answers

All 2 Replies

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);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.