The online racing simulator
Function to read ban files...Help needed!!! [ok now]
Hello again,
i can´t figure out whats going wrong with the code. I try to read the bans.ban file but the function reads the main form captions and label captions :guilty: could it be that i made a mistake with a pointer?



//---------------Header File
#ifndef __int64
#define __int64 long long
#endif

#ifndef LFS_BAN_VER
#define LFS_BAN_VER 246
#endif

struct demo_ban
{
struct in_addr ip_address;
__int64 time;
long _pad;
};
//----
struct name_ban
{
char username[24];
__int64 time;
int banhours;
int _pad;
};
//----
struct core
{
char id[7]; // null terminated string LFSBAN\0
unsigned char version;
int Demobans;
struct demo_ban *demo;
int Namebans;
struct name_ban *name;
};
//-----------------------------------Function

struct core bancore;
FILE *file = NULL;
long fsize;
int i;


memset(&bancore, 0, sizeof(core));
file = fopen("F:\\Arbeitsordner\\LFS-Remote Manager\\data\\misc\\bans.ban", "rb");

if (file != NULL)
{
fseek(file , 0 , SEEK_END);
fsize = ftell(file);
rewind(file);

// minimum size for a ban
if (fsize >= 16)
{

fread((void *)&bancore.id, 7, 1, file);
bancore.id[6] = '\0';

// read ban file version
fread((void *)&bancore.version, 1, 1, file);


// read number of bans
fread((void *)&bancore.demo, 4, 1, file);


// Check the num_demo bans is > 0
if (bancore.demo > 0)
{
// get memory to store the bans
bancore.demo = (struct demo_ban *) malloc(sizeof(struct demo_ban) * bancore.Demobans);

// read demo bans
fread((void *)bancore.demo, bancore.Demobans, sizeof(struct demo_ban), file);
}

// read number of names
fread((void *)&bancore.Namebans, 4, 1, file);

if (bancore.Namebans > 0)
{
// get memory to store the names
bancore.name = (struct name_ban *) malloc(sizeof(struct name_ban) * bancore.Namebans);

// read name bans
fread((void *)bancore.name, bancore.Namebans, sizeof(struct name_ban), file);

}


for(i = 0; i < bancore.Namebans; i++)
{
Memo2->Lines->Add(bancore.name[i].username);
}


}

}

free(bancore.demo);
free(bancore.name);
fclose(file);


Mhhh, thank you Angel! please give me a try....
I don´t got it working Angel, could you explain me your fread example please? that would be cool
I'm just about to head out, but I'll pop something together when I get back. Failing that tomrrow morning

Incidentally if anyone feels like donating a well populated bans file, that would be cool Sometimes bugs are easier to pin down with some test driven development
Thank you for your help ! i hope you can make a example based on my code....I can´t imagine why this code reads in junk....
here comes my ban file:
Attached files
bans.zip - 209 B - 225 views
It works if you change these 2 things:

struct demo_ban
{
struct in_addr ip_address;
__int64 time;
[COLOR=Red] //long _pad;[/COLOR]
};

// read number of bans
fread((void *)&bancore[COLOR=Red].Demobans[/COLOR], 4, 1, file);


// Check the num_demo bans is > 0
if (bancore.Demobans > 0)

order of 2nd and 3rd parameter at fread doesn't matter (at least in this case...)
Yeahhh!!!! it works. thank you mate
i made a great mistake.....

After that we use inet_ntoa to convert the 32-bit ip address into char for demo bans
Quote from MonkOnHotTinRoof :order of 2nd and 3rd parameter at fread doesn't matter (at least in this case...)

A very good point as the code is reading into a pointer and parsing at the later stage, but it might cause confusion in the future

Good call on the screwed struct
Thank you very much for your help!!!
i think its time to rename some stuff to make it more clearly

FGED GREDG RDFGDR GSFDG