将文件读入struct的数组,然后排序

时间:2013-11-15 02:35:51

标签: c++ arrays file

编辑:好的,所以在我下面的帖子的帮助下,我得到它编译。我将读取文件的行更改为数组:

    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;

现在我让它运行循环的第一次迭代,但随后得到分段错误。

    Currently at position24
    Segmentation fault (core dumped)

下面的原始帖子:

我又回来了。 A有一个结构和几个函数:

    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);
            }

         }
     }

现在在主要部分我创建了一个zipType[n],其中n是.bin文件中的zipTypes数。然后我打电话给binRead(&testZipType[n],&testFile);。这给了我一些你可以想象的错误。我得到了一些类似的错误:

    zipType.cpp:22:32: error: base operand of ‘->’ has non-pointer type ‘zipType’
input->read((char*)( &zip[i]->postalCode ), sizeof(int    ));

我需要做的是遍历文件并将结构的每个部分存储在数组的相应部分中。我试图做尽可能少的改变来使这项工作。我知道你们中的一些人可以*&lt; @LD(#)通往荣耀之路,但我还没有,希望能够阅读我的代码。此外,我需要在数组上做一个qsort按升序排序(我还没有尝试过这个,如果你能帮助我第一部分,我会自己给它一个镜头。)感谢您的帮助!希望很快我能在这里回馈一下!

3 个答案:

答案 0 :(得分:1)

你应该使用括号和&amp;像这样的运营商

&(zip[i]->postalCode)

您正在做的是将zip [i]转换为带有声明&amp; zip [i]的地址,然后尝试引用它。哪个给你这个错误。

-MS

答案 1 :(得分:1)

我假设zip是一个动态数组。是吗?

因此,当您声明数组时,您正在执行以下操作:

zipType* z = new zipType [10];

你的函数binRead应该声明为:

void binRead(zipType *zip, fstream *input){

当你调用binRead时,你不想要指针的地址

binRead(&z,input);

相反,你想传递你的指针,z:

binRead(z,input);

在binRead中确保像这样引用zip:

zip[i].postalCode

因为您已经传递了一个指针,所以您在binRead中所做的所有更改都将发送到zip指向的内存。

这是一个小程序,演示了我在说什么:

#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;
}

答案 2 :(得分:0)