fstream not writing, why?

Hey, ProgrammerGuy2012. Well, I'm trying to write a decompressor for an old SNES game. I already know how the compression scheme works in ASM and I'm basically trying to port it to C++, but I'm running into some problems.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;
int decompressedpointer=decompressedpointer + 0x10;
int pointer=pointer + 0x1E82EA;            //1999594=0x1E82EA aka the first compressed byte.
unsigned char numberofmoves[2];          //The variable known as $28 in the rom.
unsigned char charbyte[]={0x000000};             //This is where our hex character will be stored.
unsigned char random[1]={0x000010};
unsigned char result[2];
unsigned char result2[2];
int startbuffer=startbuffer + 0x000000;
unsigned char buffer[100];
int decompressedmoveposition;
int fromposition;
int second();
int main()
{
    buffer[0]=0x00;
    buffer[1]=0x00;
    buffer[2]=0x00;
    buffer[3]=0x00;
    buffer[4]=0x00;
    buffer[5]=0x00;
    fstream file("gg4.smc", ios::in| ios::binary);      //Read a byte from the rom
    file.seekg(pointer);
    file.read(reinterpret_cast<char*>(&charbyte), 4);
    cout<<hex<<(int)charbyte[0]<<endl;
    if (charbyte[0]>0x80){                               //If our byte is more than 80...
    numberofmoves[0]=(int)charbyte[0]&0x1F;
    cout<<hex<<(int)numberofmoves[0]<<endl;
    pointer=pointer + 1;                                 //Increase the $pointer variable...
    cout<<hex<<pointer<<endl;
    result[0]=numberofmoves[0] + random[0];      //Add the numberofmoves variable and the random variable to get the result variable
    cout<<hex<<(int)result[0]<<endl;
    if (result[0]=0x20){};                       //If result=0x20 go to a new routine
    result2[0]=(char)startbuffer+random[0];      //Add startbuffer and random 1 to get the second result.
    cout<<hex<<(int)result2[0]<<endl;
    cout<<hex<<(int)numberofmoves[0]<<endl;
    file.seekg(pointer);                        //Read the uncompressed bytes
    file.read(reinterpret_cast<char*>(&buffer), numberofmoves[0]);
    cout<<hex<<(int)buffer[0];
    fstream file2("gg4uncompressed.smc", ios::in|ios::out| ios::binary);
    file2.seekp(decompressedpointer);
    file2.write (reinterpret_cast<char*>(&buffer),numberofmoves[0]);
    while (numberofmoves[0]>0x00){
    numberofmoves[0]=numberofmoves[0]--;
    pointer=pointer++;
    decompressedpointer=decompressedpointer++;}
    cout<<"Congratulations bitches!! Your end pointer is...";
    cout<<hex<<pointer<<(int)decompressedpointer;
    random[0]=decompressedpointer;
    cout<<hex<<(int)buffer[0]<<(int)buffer[1]<<(int)buffer[2];
    system("PAUSE");
    return main();}
    else if (charbyte[0]<0x80){return second();}
    }

   int second(){
    numberofmoves[0]=charbyte[0]/0x04+0x01+0x01;   //Do this to calculate the length of the uncompressed string.
    cout<<hex<<(int)numberofmoves[0]<<endl;
    decompressedmoveposition = charbyte[1] + (charbyte[0] << 8);      //Turn the first 2 bytes of Charbyte into one integer.
    decompressedmoveposition = (decompressedmoveposition - 0x03DF)&0x03FF;
    cout<<hex<<(int)decompressedmoveposition<<endl;
    pointer=pointer + 0x02;
    result[0]=numberofmoves[0] + random[0];      //Add the numberofmoves variable and the random variable to get the result variable
    cout<<hex<<(int)result[0]<<endl;
    if (result[0]=0x20){};
    fromposition=startbuffer+decompressedmoveposition;
    cout<<hex<<(int)fromposition<<endl;
    decompressedpointer=startbuffer + random[0];
    cout<<hex<<(int)decompressedpointer<<endl;
    cout<<hex<<fromposition;
    cout<<hex<<decompressedpointer<<endl;
    numberofmoves[0]=numberofmoves[0]+0x02;
    fstream file2("gg4uncompressed.smc", ios::in|ios::out| ios::binary);
    file2.seekg(fromposition);                        //Read the uncompressed bytes
    file2.read(reinterpret_cast<char*>(&buffer), numberofmoves[0]);
    cout<<hex<<(int)buffer[0]<<(int)buffer[1]<<(int)buffer[2]<<(int)buffer[3];
    file2.seekp(decompressedpointer);
    file2.write (reinterpret_cast<char*>(&buffer),numberofmoves[0]);}


But this is mainly the part that's not working...

1
2
    file2.seekp(decompressedpointer);
    file2.write (reinterpret_cast<char*>(&buffer),numberofmoves[0]);}


For some reason, the previous READ will work, so it's reading the bytes from the rom right, but the WRITE as seen above will not work.
Last edited on
Just a comment on the visible errors.
Function main() cannot call itself.
Instead of return main(); just put return 0;

return second() doesn't make much sense as function second() is not currently returning any particular value.

if (result[0]=0x20)
Thus is an assignment statement. Most likely you wanted the comparison operator == (Two different places in the code).
Thanks, I'll fix the return thing.

Also "if (result[0]=0x20){}" is for some code that's not written yet.

My main concern though is, how I can make that last write in second() actually work?
Topic archived. No new replies allowed.