Thread: First "big" project and having problems...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    First "big" project and having problems...

    Now this is my first "big" project and im having errors executing this right. What im trying to do is right under "Therorized Algorithm".

    Noticable Problems::
    1. Doesnt create the file's
    2. Offset, Size, Name doesnt show up correctly

    I would upload the files but there big. :/


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    /*
    nbyte = null bytes between file infos
    FLbyte = Sets pointer at location in the .WAD file
    FSbyte = the 4 bytes for settign the buffer size
    FNstring = Stores the string name/Path for the file.
    
    IN THAT ORDER, with teh exception of the first.
    
    Therorized Algorithm
    
    Every Label starts with a "\" so if we read into a temp. buffer
    until we hit that variable. Then we can go back 8 BYTES and read
    FLbyte, FSbyte and end up back at the "/" (Begging of the file's name)
    then read that till it hits "0" ( NUll Byte)
    Then going into the wad and setting the offset as the one read and
    setting the buffer size as the one read. Then creating a new file where
    it dumps the memory in that file and givin the name of FNstring. 
    Repeating this until I hit 0xFFFFFF hence EOF
    
    */
    
    int main()
    {
        char* FLbyte;
        char* FSbyte;
        long  Datoff;
        long int  Datsiz;
        char*  Data;
        char ch,ch2;
        int start, end, i, set, x, point;
        char* name[MAX_PATH];
    
    
    ifstream hed ("DATAP.hed", ios::binary|ios::in);
    ifstream wad ("DATAP.wad", ios::binary|ios::in);
    
    
        if(hed.is_open())
        {               
    
           hed.seekg(0, ios::beg);//set pointer at beggining
            
             while(! hed.eof())
             {
                hed.read(reinterpret_cast<char*>(&ch), 1);
            
             if(ch = 92)
          {
               start = hed.tellg();//get start position      
             while(ch2 != 0)
             {
              hed.read(reinterpret_cast<char*>(&ch2), 1); //reading until 0    
              
              for(x = 0; x < sizeof(name); x++)
                {
                  name[x] = new char[ch2];
               }                              
              }
              }
              end = hed.tellg();//get end position
             
    
          point = (start - 8);
          hed.seekg(point);
          FLbyte = new char[4];//setting buffers sizes
          FSbyte = new char[4];
          hed.read(FLbyte, 4);
          hed.read(FSbyte, 4);//reading into buffers
          hed.seekg(end);
           
    
       Datoff = *((long int*)FLbyte);//cast to long's
       Datsiz = *((long int*)FSbyte);
       cout << "File Offset: "<< Datoff << "\nFile Size: "<<Datsiz <<" bytes\n"<<"Name: "<<name<<endl;
     
        wad.seekg(Datoff);//go to the read offset
        Data = new char[Datsiz];//set the buffer size from read size
        wad.read(Data, Datsiz);//read from there into that buffer & size
    
    
    ofstream extract(reinterpret_cast<char*>(&name), ios::binary|ios::out);
       if(extract.is_open())
       {
          extract.write(Data, Datsiz);//new file and write to it
          extract.close();
          }
       }
    }
        cout << "End Of File...";
        cin.ignore();
        cin.get();
        return 0;
    }
    There maybe some things in there that have no use, they wereleft over from previous steps.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Took the liberty of reformatting the code due to formatting issues (alignment of brackets):
    Code:
    int main()
    {
        char* FLbyte;
        char* FSbyte;
        long  Datoff;
        long int  Datsiz;
        char*  Data;
        char ch,ch2;
        int start, end, i, set, x, point;
        char* name[MAX_PATH];
    
        ifstream hed ("DATAP.hed", ios::binary|ios::in);
        ifstream wad ("DATAP.wad", ios::binary|ios::in);
    
        if(hed.is_open())
        {               
    
            hed.seekg(0, ios::beg);//set pointer at beggining
            
            while(! hed.eof())
            {
                hed.read(reinterpret_cast<char*>(&ch), 1);
            
                if(ch = 92)
                {
                    start = hed.tellg();//get start position      
                    while(ch2 != 0)
                    {
                        hed.read(reinterpret_cast<char*>(&ch2), 1); //reading until 0    
                        for(x = 0; x < sizeof(name); x++)
                        {
                            name[x] = new char[ch2];
                        }                              
                    }
                }
                end = hed.tellg();//get end position
             
                point = (start - 8);
                hed.seekg(point);
                FLbyte = new char[4];//setting buffers sizes
                FSbyte = new char[4];
                hed.read(FLbyte, 4);
                hed.read(FSbyte, 4);//reading into buffers
                hed.seekg(end);
           
                Datoff = *((long int*)FLbyte);//cast to long's
                Datsiz = *((long int*)FSbyte);
                cout << "File Offset: "<< Datoff << "\nFile Size: "<<Datsiz <<" bytes\n"<<"Name: "<<name<<endl;
     
                wad.seekg(Datoff);//go to the read offset
                Data = new char[Datsiz];//set the buffer size from read size
                wad.read(Data, Datsiz);//read from there into that buffer & size
                ofstream extract(reinterpret_cast<char*>(&name), ios::binary|ios::out);
                if(extract.is_open())
                {
                    extract.write(Data, Datsiz);//new file and write to it
                    extract.close();
                }
            }
        }
        cout << "End Of File...";
        cin.ignore();
        cin.get();
        return 0;
    }
    1. There is no need to seek after opening the file, the "get" pointer should already be set to the beginning of the file.

    2. You did not initialize ch2, make sure it isn't 0 to start with.

    3. Taking the address of a char (&ch, &ch2) yields a character pointer... which does not need to be cast into a... character pointer.

    4. The tellg member function's return type is streampos, not int. On my system this happens to be a typedef'd long.

    5. You should not be using hed.eof() to control the execution of the while loop... the FAQ has more on the reasons why. You'd probably want to place the hed.read line from directly below that into the while loop instead.

    6.
    Code:
    while(ch2 != 0)
    {
        hed.read(reinterpret_cast<char*>(&ch2), 1); //reading until 0    
        for(x = 0; x < sizeof(name); x++)
        {
            name[x] = new char[ch2];
        }                              
    }
    There are some serious issues with this bit of code. Let's say that ch2 is not 0 to start with so we enter into the while loop. Now, regardless of what ch2 is from the read statement, you loop through name and allocate MAX_PATH character arrays of size ch2 to each index of name. If ch2 was 0 from the read, this means you've called new char[0] several times and then exit the while loop which is useless. If ch2 was not 0, then you call new char[ch2] a bunch of times and then go back again through the while loop where you will immediately call new char[ch2] again overwriting the existing pointers and leaking memory.

    7.
    Code:
    FLbyte = new char[4];//setting buffers sizes
    FSbyte = new char[4];
    hed.read(FLbyte, 4);
    hed.read(FSbyte, 4);//reading into buffers
    hed.seekg(end);
           
    Datoff = *((long int*)FLbyte);//cast to long's
    Datsiz = *((long int*)FSbyte);
    Why don't you just read the values from the file into the long's (this is where the reinterpret_cast is usefull):
    Code:
    hed.read(reinterpret_cast<char*>(&Datoff),sizeof Datoff);
    hed.read(reinterpret_cast<char*>(&Datsiz),sizeof Datsiz);
    Then you can get rid of FLbyte and FSbyte.

    8.
    Code:
    ofstream extract(reinterpret_cast<char*>(&name), ios::binary|ios::out);
    With how you've set up name I'm not surprised this would not work. name is an array of character pointers, not a character array or a character pointer as the ofstream constructor expects, it is of the wrong type (there is a difference, perhaps subtle).

    9. You perform no cleanup of memory you've allocated.

    I'm tired, you've got some stuff to consider...

    [edit]Made a correction about reading into Datoff/Datsiz since first posting[/edit]
    Last edited by hk_mp5kpdw; 02-11-2006 at 09:54 PM. Reason: Corrected comment about reading into Datoff and Datsiz
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed