Help revising

This is a discussion on Help revising within the C++ Programming forums, part of the General Programming Boards category; I need help revising this code, i've tryed to get it to work but nothings worked for me. Any help? ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

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

  2. #2
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    I have no idea, I have no compiler on this computer. Maybe you could post the error.
    And you tabbed it all wrong...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    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.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > 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.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    > 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.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Well you managed to allocate space for data, try doing something similar for name.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Ah, I had that the first time.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I don't see where you added the allocation for name.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > 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.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    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;
    }

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > while(pnt!=0x00) hed.read(&pnt, 1);
    This terminates when pnt == 0

    > while(pnt!=0x00)
    pnt is still 0, so it does nothing.

Popular pages Recent additions subscribe to a feed

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