alright. I have this file that lists about 3000 coordinates in this way.

-90.0000 0.0000 220.7533
-90.0000 5.0000 220.7533
-90.0000 10.0000 220.7533
-90.0000 15.0000 220.7533


I'm trying to open this file in my program, but I'm having no luck at all. I've tried reading numerous threads on doing so, but after hours and hours of frustration, nothing has worked.

So far I have this

#include <fstream>
#include <string>
using namespace std;

int lat;
int lon;
int R;

int main()
{
    ifstream in("4vesta.txt");
    string line;
    while( getline(lat, lon, R) )
    {
          cout << lat << lon << R << endl;
    }
}

I mainly want the code to read those numbers, so I can set it up as an array.

Thanks.

getline() doesn't work like that. What you want is this:

while( in >> lat >> lon >> R)
{
   // do something with that data

}

Now, if you want to keep all the data in memory at the same time, create a structure and then a vector of structures

#include <vector>
...
struct data
{
    int lat;
    int lon;
    int R;
};

vector<data>  theArray;
data d;
while( in >> d.lat >> d.lon >> d.R)
{
    theArray.push_back(d);
}

This is what I have now. I'm still having trouble opening the file I want.

#include <fstream>
#include <string>
using namespace std;

int lat;
int lon;
int R;

int main()
{
    ifstream in("4vesta.txt");
    string line;
   while( in >> lat >> lon >> R) )
    {
          cout << lat << lon << R << endl;
    }
    return 0;
}

The build is succeeding but its not showing anything on the screen once I compile it.

There's a stray bracket in your while loop, also i think you should be using doubles not ints...

Chris

Yes, I fixed those things. Here is the code now.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

float lat;
float lon;
float R;

int main()
{
    ifstream in("4vesta.txt");
    string line;
    while( in >> lat >> lon >> R )
    {
          cout << lat << lon << R << endl;
    }

    return 0;
}

So as I said earlier, I have about 3000 numbers in a separate .txt file written in this way:

  -90.0000   10.0000  220.7533
  -90.0000   15.0000  220.7533
  -90.0000   20.0000  220.7533
  -90.0000   25.0000  220.7533
  -90.0000   30.0000  220.7533
  -90.0000   35.0000  220.7533
  -90.0000   40.0000  220.7533

These numbers are not coming up when I run the program. It's just blank.

My guess is the file is not open. Add this after the open

if( !in.is_open())
{
   cout << "Failed to open the file\n";
   return 1;
}

Yes I think you are right. The file is not open. After running it comes up as not being able to open the file.

What do I have to do in order to open the file.

Thanks for all the help guys!

you have to know where the file is. Add the full path to see if the program will open it. ifstream in("c:\\myproject\\4vesta.txt");

Alright I tried going to the file and right click on it to copy the full path in order to get to it. So now I have this

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

float lat;
float lon;
float R;

int main()
{

    ifstream in("C:\Users\Yaser\Documents\Visual Studio 2008\Projects\Vesta Coordinates\4vesta.txt");

    if( !in.is_open())
    {
        cout << "Failed to open the file\n";
        return 1;
    }

    string line;

    while( in >> lat >> lon >> R )
    {
          cout << lat << lon << R << endl;
    }

    return 0;
}

Its still not opening though. It still fails to open!! Hmmm...........

>>ifstream in("C:\Users\Yaser\Documents\Visual Studio 2008\Projects\Vesta Coordinates\4vesta.txt");

I suppose you ignored all the errors and warnings your compiler gave you on that line ???? You have to double-backslash the directory separators ifstream in("C:\\Users\\Yaser\\Documents\\Visual Studio 2008\\Projects\\Vesta Coordinates\\4vesta.txt");

IT WORKS!!! I really appreciate the help man. You just made me one happy person. I have one more question about this though.

So this is going to put my data set in one memory space?

Then I could just use my data set as an array? and when I'm using this array in the program it will look like this "theArray[...][3]??

Thanks! once again.


#include <vector>
...
struct data
{
int lat;
int lon;
int R;
};

vector<data> theArray;
data d;
while( in >> d.lat >> d.lon >> d.R)
{
theArray.push_back(d);
}

>>it will look like this "theArray[...][3]??

No. It is a one-dimensional array of structures. So it looks like this theArray[...]

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.