Reputation: 139
EDIT: Ok, so with some help from the post below me, I got it to compile. I changed the lines that read the file into the array to this:
input->read((char*)( &(zip[i].postalCode) ), sizeof(int ));
input->read((char*)( &junk ), sizeof(int ));
input->read((char*)( &(zip[i].longitude) ), sizeof(double ));
input->read((char*)( &(zip[i].latitude) ), sizeof(double ));
cout << "Currently at position" << input->tellg() << endl;
Now I get it to run through the first iteration of the loop, but then get a segmentation fault.
Currently at position24
Segmentation fault (core dumped)
ORIGINAL POST BELOW:
I'm back at it again. A have a struct and a couple functions:
struct zipType{
int postalCode;
double longitude;
double latitude;
};
void zipToCout(zipType zip){
cout << "Postal Code = " << zip.postalCode << "\tLongitude = " << zip.longitude << "\t\tLatitude = " << zip.latitude << endl;
}
void binRead(zipType *zip[], fstream *input){
int junk;
int count;
int end;
input->seekg(0,ios::end);
end=input->tellg();
count=input->tellg()/24;
input->seekg(0);
while(input->tellg()!=end){
for(int i=0;i<count;i++){
input->read((char*)( &zip[i]->postalCode ), sizeof(int ));
input->read((char*)( &junk ), sizeof(int ));
input->read((char*)( &zip[i]->longitude ), sizeof(double ));
input->read((char*)( &zip[i]->latitude ), sizeof(double ));
cout << "Currently at position" << input->tellg() << endl;
//zipToCout(*zip);
}
}
}
Now in the main I created a zipType[n]
where n is the number of zipTypes in the .bin file. I then called binRead(&testZipType[n],&testFile);
. This gives me some errors as you can imagine. I get a few errors similar to this:
zipType.cpp:22:32: error: base operand of ‘->’ has non-pointer type ‘zipType’
input->read((char*)( &zip[i]->postalCode ), sizeof(int ));
What I need to do, is to go through the file and store each portion of the struct in its appropriate portion of the array. I'm trying to make as little changes as possible to make this work. I know some of you can *<@LD(#) your way to glory, but I'm not there yet and want to be able to read my code. Also, i need to do a qsort on the array to sort it in ascending order (I haven't tried this yet and if you can help me with the first part I'll give it a shot myself.) Thanks for any help! Hopefully soon I'll be able to give back a little around here!
Upvotes: 0
Views: 176
Reputation: 372
I'm assuming that zip is a dynamic array. Is that right?
So when you declare your array, you're doing something along the lines of:
zipType* z = new zipType [10];
Your function binRead should be declared as such:
void binRead(zipType *zip, fstream *input){
When you call binRead, you don't want to the address of the pointer
binRead(&z,input);
Instead you want to pass your pointer, z:
binRead(z,input);
Inside of binRead make sure to reference zip like so:
zip[i].postalCode
Because you've passed a pointer, all changes you make in binRead will be made to the memory that zip points to.
Here's a small program that demonstrates what I'm talking about:
#include <iostream>
struct zipType{
int postalCode;
double longitude;
double latitude;
};
void blah(zipType *zip){
for (int i=0;i<10;i++){
std::cout << zip[i].postalCode << std::endl;
}
zip[0].postalCode = 60626;
}
int main(int argc, const char * argv[])
{
zipType* z = new zipType [10];
for(int i=0;i<10;i++){
z[i].longitude = i * 10;
z[i].latitude = i * 5;
z[i].postalCode = i * 6;
}
blah(z);
std::cout << z[0].postalCode;
delete [] z;
}
Upvotes: 1
Reputation: 60017
Use vector to store the data - http://www.cplusplus.com/reference/vector/vector/
Then use sort - http://www.cplusplus.com/reference/algorithm/sort/
Job done
Upvotes: 0
Reputation: 146
You should use brackets with & operator like this
&(zip[i]->postalCode)
What you are doing is you are converting zip[i] to an address with statement &zip[i] and then trying to de reference it. Which is giving you this error.
-MS
Upvotes: 1