Page 1 of 1

FILE IO Error

PostPosted: Sat Oct 22, 2011 4:40 am
by EvanBlack
Error line 16 Incompatible Types: Cannot convert from 'double' to ' * const char '

Code: Select all
typedef struct NewStruct
{
    int a;
    int b;
    char c[20];
} NewStruct;

NewStruct myStruct;
FILE *g_DATAMAPFILE;

NewStruct * pMyStruct = &myStruct;


int SaveDataMap()
{
    g_DATAMAPFILE = fopen("Level.txt", r+b); //fopen error
    if(g_DATAMAPFILE == NULL) {return 1;}
    fwrite(pMyStruct, sizeof(*pMyStruct), 1, g_DATAMAPFILE);
    fclose(g_DATAMAPFILE);
    return 0;
   
}

void init()
{
    pMyStruct->a = 20;
    pMyStruct->b = 55;
    strcpy(pMyStruct->c, "String");
    if(!SaveDataMap()){ExitGame();}
}

Re: FILE IO Error

PostPosted: Sat Oct 22, 2011 5:05 am
by skydereign
You need the format to be a const char*. "r+b", not r+b.
Code: Select all
typedef struct NewStruct
{
    int a;
    int b;
    char c[20];
} NewStruct;

NewStruct myStruct;
FILE *g_DATAMAPFILE;

NewStruct * pMyStruct = &myStruct;


int SaveDataMap()
{
    g_DATAMAPFILE = fopen("Level.txt", "r+b"); // fopen error fixed
    if(g_DATAMAPFILE == NULL) {return 1;}
    fwrite(pMyStruct, sizeof(*pMyStruct), 1, g_DATAMAPFILE);
    fclose(g_DATAMAPFILE);
    return 0;
   
}

void init()
{
    pMyStruct->a = 20;
    pMyStruct->b = 55;
    strcpy(pMyStruct->c, "String");
    if(!SaveDataMap()){ExitGame();}
}

Re: FILE IO Error

PostPosted: Sat Oct 22, 2011 5:49 am
by EvanBlack
Oh thanks! I was trying to figure out why it said i couldnt use w lol thanks! I must have over looked it.

Re: FILE IO Error

PostPosted: Mon Oct 24, 2011 5:33 am
by EvanBlack
Having trouble figuring out how to save and load from an array of structs... Here is my code.

I changed it a couple of times to try different things and nothing worked. Here is the last thing I tried.


BINSAVE.ged Script:
Code: Select all
typedef struct NewStruct
{
    int a;
    int b;
    char c[20];
} NewStruct;

NewStruct myStruct;
NewStruct myStruct2;
FILE *g_DATAMAPFILE;
NewStruct * pMyStruct[2];


int SaveDataMap()
{
    g_DATAMAPFILE = fopen("Level.txt", "wb");
    if(g_DATAMAPFILE == NULL) {return 1;}
    fwrite(pMyStruct, sizeof(NewStruct)*2, 1, g_DATAMAPFILE);
    fclose(g_DATAMAPFILE);
    return 0;
 
}

void init()
{
    pMyStruct[0] = &myStruct;
    pMyStruct[1] = &myStruct2;
 
    pMyStruct[0]->a = 20;
    pMyStruct[0]->b = 55;
    strcpy(pMyStruct[0]->c, "String");
 
    pMyStruct[1]->a = 99999;
    pMyStruct[1]->b = 77777;
    strcpy(pMyStruct[1]->c, "LOLFUCKLMAO");
 
    if(!SaveDataMap()){ExitGame();}
}




BINLOAD.ged Script:
Code: Select all

typedef struct NewStruct
{
    int a;
    int b;
    char c[20];
} NewStruct;

NewStruct myStruct;
NewStruct myStruct2;
FILE *g_DATAMAPFILE;
NewStruct * pMyStruct[2];


int LoadDataMap()
{
    g_DATAMAPFILE = fopen("Level.txt", "rb");
    if(g_DATAMAPFILE == NULL) {return 1;}
    fread(pMyStruct, sizeof(*pMyStruct[0]) * 2, 2, g_DATAMAPFILE);
    fclose(g_DATAMAPFILE);
    return 0;
 
}

void init()
{
    pMyStruct[0] = &myStruct;
    pMyStruct[1] = &myStruct2;
    if(LoadDataMap())
    {
        ExitGame();
    }
}



Edit: I got it to work using a for loop, but how do I do it without a loop?