-
Help revising
I need help revising this code, i've tryed to get it to work but nothings worked for me. Any help?
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char pnt;
char *name;
char *data;
int x = 0, index, endex = 0;
long size, offset;
ifstream hed("datap.hed", ios::in | ios::binary);
ifstream wad("datap.wad", ios::in | ios::binary);
while(! hed.eof())
{
hed.seekg(endex);
while(pnt!=0x5C) hed.read(&pnt, 1);
index = hed.tellg();
while(pnt!=0x00)
{
hed.read(&pnt, 1);
name[x] = pnt;
x++;
}
endex = hed.tellg();
if(strlen(name) > 4)
{
hed.seekg(index-9);
hed.read(reinterpret_cast<char*>(&offset), 4);
wad.seekg(offset);
hed.read(reinterpret_cast<char*>(&size), 4);
data = new char[size];
wad.read(data,size);
ofstream extract(name, ios::out | ios::binary);
extract.write(data, size);
extract.close();
data = 0;
cout << "\n" << name;
} else endex++;
x = 0;
}
wad.close();
hed.close();
return 0;
}
-
I have no idea, I have no compiler on this computer. Maybe you could post the error.
And you tabbed it all wrong...
-
EXECUTING:
/home/rab/Files/code/c++/wadrip/wadrip
----------------------------------------------
----------------------------------------------
Program has been terminated receiving signal 11 (Segmentation fault)
Press the Enter key to close this terminal ...
Thats the error i get when i run it. No errors when i compile it.
-
> while(pnt!=0x5C) hed.read(&pnt, 1);
You don't initialise pnt, so no telling what this loop will do first time around
> name[x] = pnt;
name doesn't point anywhere specific, so who knows what memory you're trashing.
This is the most likely cause of the segfault, if the pointer just happens to be pointing outside the address space.
> while(! hed.eof())
Read the FAQ.
> data = 0;
You delete memory by doing
delete [ ] data;
Just assigning 0 is a memory leak.
-
> name[x] = pnt;
name doesn't point anywhere specific, so who knows what memory you're trashing.
This is the most likely cause of the segfault, if the pointer just happens to be pointing outside the address space.
Well , what do you suggest to fix this? I've fixed everything else.
-
Well you managed to allocate space for data, try doing something similar for name.
-
Ah, I had that the first time.
-
Code:
#include <iostream>
#include <fstream>
using namespace std;
char *data;
int main()
{
char pnt = 0;
char *name;
int x = 0, index, endex = 0;
long size, offset;
ifstream hed("datap.hed", ios::in | ios::binary);
ifstream wad("datap.wad", ios::in | ios::binary);
while(! hed.eof())
{
hed.seekg(endex);
while(pnt!=0x5C) hed.read(&pnt, 1);
index = hed.tellg();
while(pnt!=0x00)
{
hed.read(&pnt, 1);
name[x] = pnt;
x++;
}
endex = hed.tellg();
if(strlen(name) > 4)
{
hed.seekg(index-9);
hed.read(reinterpret_cast<char*>(&offset), 4);
wad.seekg(offset);
hed.read(reinterpret_cast<char*>(&size), 4);
data = new char[size];
wad.read(data,size);
ofstream extract(name, ios::out | ios::binary);
extract.write(data, size);
extract.close();
delete[] data;
cout << "\nExtracted: " << name <<"(Size: "<<size<<" Offset: 0x"<<hex<<offset<<")";
} else endex++;
delete[] name;
x = 0;
}
cout << "Done";
wad.close();
hed.close();
return 0;
}
I could'nt have *name and *data or i recieved a segfault.
It breaks and gives the error
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Heres the file, just comment out the wad and extract part. -->hedfile
-
I don't see where you added the allocation for name.
-
> delete[] name;
So where is the
name = new char[1000];
Of course, if you were doing it properly, you would do while(pnt!=0x00) twice, the first time to work out the length of the name you need to allocate.
Also, making data a global adds no value at all.
-
But it can't read the name right now, it either gives no name or invalid characters.
Code:
#include <iostream>
#include <fstream>
using namespace std;
char *data;
int main()
{
char pnt = 0;
char *name;
int x = 0, index, endex = 0,hsize;
long size, offset;
ifstream hed("datap.hed", ios::in | ios::binary);
ifstream wad("datap.wad", ios::in | ios::binary);
hed.seekg(0, ios::end);
hsize = hed.tellg();
hsize = (hsize - 8);
while(endex != hsize)
{
hed.seekg(endex);
while(pnt!=0x5C) hed.read(&pnt, 1);
index = hed.tellg();
while(pnt!=0x00) hed.read(&pnt, 1);
endex = hed.tellg();
if((endex-index) > 4)
{
hed.seekg(index);
name = new char[(endex-index)];
while(pnt!=0x00)
{
hed.read(&pnt, 1);
name[x] = pnt;
x++;
}
hed.seekg(index-9);
hed.read(reinterpret_cast<char*>(&offset), 4);
wad.seekg(offset);
hed.read(reinterpret_cast<char*>(&size), 4);
data = new char[size];
wad.read(data,size);
ofstream extract(name, ios::out | ios::binary);
extract.write(data, size);
extract.close();
delete[] data;
cout << "\nExtracted: " << name <<"(Size: "<<size<<" Offset: 0x"<<hex<<offset<<")";
} else endex++;
delete[] name;
x = 0;
}
cout << "Done";
wad.close();
hed.close();
return 0;
}
-
> while(pnt!=0x00) hed.read(&pnt, 1);
This terminates when pnt == 0
> while(pnt!=0x00)
pnt is still 0, so it does nothing.