0

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!

1
  • Also if possible, I'd like to make the binRead function void binRead(zipType *zip, fstream *input) instead of void binRead(zipType *zip[], fstream *input) Commented Nov 15, 2013 at 2:37

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This makes a lot of sense, I think I have some things wrong with my pointers, I'm going to change some things based on this and I'll +1 if I can get it working thank you!
Thank you very much, I changed the way I was pointing in main and everything flowed nicely. Thank you!
1

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

1 Comment

I tried this: input->read((char*)( &(zip[i]->postalCode) ), sizeof(int )); and still have the same error. What your saying makes sense but I can't figure out how to make it work lol. I'm so bad with pointers, and get * and & mixed up
0

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

1 Comment

I'm supposed to use an array for this exercise, but I will read the above posts, as I'm sure it will clear things up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.