type casting-Linux vs Unix

This is a discussion on type casting-Linux vs Unix within the C++ Programming forums, part of the General Programming Boards category; I wrote the following program which implements the SORT phase of the external mergesort on the file given as the ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    type casting-Linux vs Unix

    I wrote the following program which implements the SORT phase of the external mergesort on the file given as the 2nd argument in the command line :

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    typedef unsigned char byte;
    
    const int BLOCKSIZE=512;
    const int Nb=3;
    
    struct Customer {
        char id[8];
        char fname[13];
        char lname[21];
        char street[13];
        unsigned int postcode;
        unsigned short number;
        char type;
    };
    
    struct Code {
        unsigned short int postcode;
        char city[14];
    };
    
    class MyCompare {
    public:
        int operator()(const Code& c1,const Code& c2) const {
            return c1.postcode<c2.postcode;
        }
    };
    
    // Gets the length bytes from Block starting from offset putting them in value
    
    void getField(byte* Block,int length,int offset,void* value) {
        Block+=offset;
        memcpy(value,Block,length);
    }
    
    // Sets the length bytes from Block starting from offset into value
    
    void setField(byte *Block,int length,int offset,void *value) {
        Block+=offset;  
        memcpy(Block,value,length);
    }
    
    void sortfile(ifstream& infile,int recsize,int field,fstream& outfile) {
    
        byte* block=new byte[BLOCKSIZE];
        int recnum=BLOCKSIZE/recsize;
        infile.seekg(0,ios::end);
        int blocknum=int(ceil(float(infile.tellg())/float(BLOCKSIZE)));
        infile.seekg(0,ios::beg);
    //    int Nr=int(ceil(float(blocknum)/float(Nb)));
        int i=1;
        while(!infile.eof()) {
            vector<Code> code;
            for(int j=1;j<=recnum*Nb;j++) {
                Code tmpcode;
                infile>>tmpcode.postcode>>tmpcode.city;
                if(infile.eof()) break;
                code.push_back(tmpcode);
            }
            sort(code.begin(),code.end(),MyCompare());
            string name(1,char(48+i));name="code"+name;
            fstream run(name.c_str(),ios::binary|ios::out|ios::in|ios::trunc);
            for(int j=0;j<int(ceil(float(code.size())/float(recnum)));j++) {
                for(int k=0;k<recnum && j*recnum+k<code.size();k++) {
                    setField(block,2,k*recsize,&code[j*recnum+k].postcode);
                    setField(block,14,k*recsize+2,code[j*recnum+k].city);
                }
                run.write(block,BLOCKSIZE);  // *********
                memset(block,0,BLOCKSIZE);
            }
            i++;
        }
        ifstream in1("code1");
        for(int j=0;j<3;j++) {
            in1.read(block,BLOCKSIZE);   // ****************
            for(int i=0;i<32;i++) {
                int postcode;char city[14];
                getField(block,2,i*recsize,&postcode);
                getField(block,14,i*recsize+2,city);
                cout<<postcode<<' '<<city<<endl;   // $$$$$$$$$$$
            }
        }
        ifstream in2("code2");
        in2.read(block,BLOCKSIZE);    // *****************
        for(int i=0;i<2;i++) {
            int postcode;char city[14];
            getField(block,2,i*recsize,&postcode);
            getField(block,14,i*recsize+2,city);
            cout<<postcode<<' '<<city<<endl;   // $$$$$$$$$$
        }
    
        delete[](block);    
    }
    
    int main(int argc,char** argv) {
    
        if(argc!=4) {
            cout<<"Usage:as1 <customer_file> <code_file> <city>"<<endl;
            exit(1);
        }
        ifstream custfile(argv[1]);
        if(!custfile) {
            cout<<"Could not open file "<<argv[1]<<endl;
            exit(1);
        }
        ifstream codefile(argv[2]);
        if(!codefile) {
            cout<<"Could not open file "<<argv[2]<<endl;
            exit(1);
        }
        string city=argv[3];
    
        fstream outfile("test21ord",ios::binary|ios::out|ios::in|ios::trunc);
        sortfile(codefile,sizeof(Code),6,outfile);
    }
    In Linux it runs perfectly.
    In Unix(Solaris) it prints 3 compile time errors in the lines with the asterisks.
    It's because it needs block to be type casted to char *.
    I then compile it putting (char *)block in these 3 lines but when i run it i encounter another problem.
    In the lines with the dollars instead of printing the postcode it prints a huge number(maybe the address of postcode in memory,i don't know).The city is printed as it should.I guess because it is a char array.
    What has to be changed?
    Here is a file to test my prog.Put its name as the 2nd argument.
    For the 1st argument just put a valid filename and for the 3rd just put a string.
    The file :


    157 ATHINA
    155 ATHINA
    344 LARISA
    164 ATHINA
    153 ATHINA
    345 LARISA
    324 TRIKALA
    322 KARDITSA
    323 TRIKALA
    445 HRAKLEIO
    435 XANIA
    845 BOLOS
    241 XANIA
    109 AG.NIKOLAOS
    199 RODOS
    220 KERKYRA
    139 THESSALONIKI
    963 PATRA
    331 PATRA
    287 THESSALONIKI
    222 XANTHI
    444 XANTHI
    901 IOANNINA
    535 IOANNINA
    112 RODOS
    800 RODOS
    900 AG.NIKOLAOS
    400 BOLOS
    200 XALKIDA
    676 XALKIDA
    701 AIGINA
    401 SPARTH
    131 PYRGOS
    210 SPARTH
    101 TRIPOLI
    450 TRIPOLI
    302 KARDITSA
    303 KWS
    502 KALYMNOS
    229 RODOS
    228 PATRA
    226 IOANNINA
    306 KILKIS
    307 KILKIS
    402 TRIKALA
    410 THIVA
    419 XALKIDA
    503 KILKIS
    506 GIANNITSA
    808 SKOPELOS
    841 SKIATHOS
    842 PAROS
    998 NAKSOS
    922 KORINTHOS
    555 KORINTHOS
    777 HRAKLEIO
    481 HRAKLEIO
    311 NAFPLIO
    440 NAFPLIO
    111 LESBOS
    098 SAMOS
    012 XIOS
    009 SANTORINH
    022 ERMOYPOLH
    066 LEROS
    091 KASOS
    077 KARPATHOS
    020 KASTELORIZO
    062 SKIATHOS
    765 KORINTHOS
    124 KIATO
    345 LEMESOS
    456 AMMWXOSTOS
    567 AG.KYRIKOS
    678 EYDILOS
    789 KARLOBASI
    891 VATHI
    005 LINDOS
    033 PATRA
    019 ITHAKI
    090 KEFALLONIA
    050 ZAKINTHOS
    911 RODOPI
    167 XANIA
    898 MESOLOGI
    232 MESOLOGI
    676 AITWLIKO
    430 KARPENHSI
    987 KASTORIA
    876 KASSANDRA
    765 RETHIMNO
    654 RETHIMNO
    543 PITHAGOREIO
    432 MARATHOKAMPOS
    321 ATHINA
    109 RETHIMNO
    666 DILESI
    176 THIVA
    Last edited by k_w_s_t_a_s; 05-14-2003 at 04:04 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    Thanks salem.It worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type casting object to int?
    By symbiote in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 10:46 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 08:51 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21