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]