-
How to read files
I want to read a bunch of numbers from a file.
I have declared a few structures in structures
I passed a pointer from main to a local function and declared a dunamic array in it.
int readData(Point base[2], Vertex* vertices[])
{
int nPts = 0;
Vertex *vertex = new Vertex[48];
Vertex *pVertex = &vertex[0]; <----should I be using this pointer to read the values?
......
cout<<vertex[0].bldgId;
fin>>Vertex[0].bldgId;
cout<<vertex[0].bldgId;
}
I am trying to figure out how to read 1 of the values bldgId to the
dynamic array. The value in the file is '1' but it reads '4475424' for both cout statements.
My guess is its not reading it correctly? I think I declared and opened the file correctly. How could I read in the value?
Thx
-
Assuming your file is a text file (readable with an editor) what you have should work (no need for a pointer).
It's possible the file never got opened (maybe check to be sure):
Code:
fin.open(filename, ios::in);
if (!fin)
{
cout "Error opening file:" << filename << endl;
return;
}
-
Ok I put these lines before the fin part but it still doesnt work.
ifstream fin("survey.dat");
if (!fin)
{
cout<<"Error opening file:"<<"survey.dat"<<endl;
exit(1);
}
fin.open("survey.dat", ios::in); <--- if I place this where u said it give the error message so I put it here.
Why wont the file open?!
-
>ifstream fin("survey.dat");
This line actually opens the file, so you don't need the fin.open(). Use one or the other.
If you are getting the error message "Error opening file", then it probably is looking in the wrong directory, so try putting the full path. For example, on a windows computer, something like:
ifstream fin("\\dir\\subdir\\survey.dat");
-
Ok cool that worked but I forgot I actually need to read them into the pointer vertices[]
Code:
while (! fin.eof())
{
fin>>vertices[nPts]->bldgId;
nPts++;
}
It keeps giving me "Unhandled Exception" error! I'm sure thats how to read pointers? Am I missing something?
-
If you actually need to pass this newly allocated array back to your main() function, then you will need a double pointer. Your code will look similar to this.
Code:
int readData(Point base[2], Vertex** vertices);
int main(void)
{
Point base[2];
Vertex *vertices;
int i;
readData(base,&vertices);
for (i=0; i<48; i++)
cout << vertices[i].bldgId << endl;
//Clean up memory when done
delete []vertices;
return 0;
}
int readData(Point base[2], Vertex** vertices)
{
int nPts = 0;
int i;
*vertices = new Vertex [48];
ifstream fin("survey.dat");
if (!fin)
{
cout << "Could not find file.\n";
return 0;
}
while (fin>>(*vertices)[nPts].bldgId)
{
cout<<(*vertices)[nPts].bldgId << endl;
nPts++;
}
cout << "nPts:" << nPts << endl;
return 1;
}
You can use a single pointer (Vertex *vertices) if you allocate the array in the main() function.
Also be sure that nPts is 48 at the end of the function, otherwise you may be going past the end of the array.
If nPts is your return value, then change the last line above to:
return nPts;
-
Thx swoopy. I don't think I need double pointers. Don't even know what that is!
I figured it out though. Just needed to dynamically declare vertices in the loop.
And now I am trying to do a bubble sort of the data read in but it's giving me grabage values. w/o the bolded lines of code, the output is correct (unsorted of course) so is it not possible to swap structures like that?
Code:
// Sort the vertices according to the angle of the relative polar coordinates
void sortVertices(Vertex* vertices[], int nPts)
{
Vertex temp;
bool exchange;
do
{
exchange = false;
for (int counter=0; counter < nPts; counter++)
if (vertices[counter]->polar.theta > vertices[counter+1]->polar.theta)
{
vertices[counter] = new Vertex;
temp = *vertices[counter];
*vertices[counter] = *vertices[counter+1];
*vertices[counter+1] = temp;
exchange = true;
cout<<vertices[counter]->polar.d<<" "<<vertices[counter]->polar.theta<<" "<<endl;
}
nPts--;
}
while (exchange == true);
}
-
I'm glad you got the allocation figured out. For the swap, normally you would have to swap each individual part of the structure. However in your case I believe all you need to do is swap the pointers, so this should work.
Code:
//Declare temp as:
Vertex *temp;
//Change the swap statements within the loop to this:
temp = vertices[counter];
vertices[counter] = vertices[counter+1];
vertices[counter+1] = temp;
And leave this statement out:
vertices[counter] = new Vertex;
-
ofstream fin("filename.txt");
if (fin.fail()) {
cout << "\nCouldn't find the file" << endl; }