Help with VC files...

Started by
4 comments, last by habus 24 years, 4 months ago
I just tried that code (in VC 6) and it works fine, the contents of s is "@ABCDEFGHIJKL".

Perhaps you could provide more sample code that should demonstrate your problem.

Advertisement
Ok, im posting the code to create my file and read it. I'm using VC 5 and it is driving me crazy :

// My structure
struct struObj
{
__int16 iPosx, iPosy ;
__int16 iFrame, iMaxFrame;
__int16 iHp,iMaxHp;
__int16 iTipoObj,iDireccion;
__int16 iSpeedx,iSpeedy;
__int16 iTamX,iTamY,iValida;
};

struObj objTemp;
*FILE *fd;

// I create my file here

if ((fd=fopen("c:\\cheerio2.map","r+b"))==NULL)
{
//strcpy( CGame.sError, "Cant create map" );
}

objTemp.iPosx=64;
objTemp.iPosy=65;
objTemp.iFrame=66;
objTemp.iMaxFrame=67;
objTemp.iHp=68;
objTemp.iMaxHp=69;
objTemp.iTipoObj=70;
objTemp.iDireccion=71;
objTemp.iSpeedx=72;
objTemp.iSpeedy=73;
objTemp.iTamX=74;
objTemp.iTamY=75;
objTemp.iValida=76;
fwrite(&objTemp,sizeof( objTemp ), 1, fd );

fclose( fd);

// here I attemp to read it

if ((fd=fopen("c:\\cheerio2.map","r+b"))==NULL)
{
// strcpy( CGame.sError, "Error in cheerio2.map" );
}

fread(&objTemp,sizeof( objTemp ), 1, fd );

// check the values for objTemp after reading the file

fclose( fd);

Hope someone could help me..

"Old programmers don't die,they just terminate and stay resident."
Um, I tried compiling your code in VC++ 5.0 and it worked just fine. Well other than the fact that your first fread has a "r+b" instead of "w+b".

I noticed that the file format you specify in your last post doesn't match the one you specified in the first post. Is your VB code outputting properly? Or could it be an error in your check to make sure the file read properly?

Yup, It should be w+b in the first fopen and the reason I changed the file format is due I was
trying to open it in binary or text format, but I was getting the same error in both of them.
The output VB generates is equal to that VC does - with the fwrite posted before - my problem is
when I open the file. - Im checking directly the struct in the debugger -.
By the way I put my code before the statement that creates the mainwindow, does it affect in any
way?

Thanks!

"Old programmers don't die,they just terminate and stay resident."
Hi! Im trying to load a little file containing the info of my lvl editor - which was done in VB :P - but when I read them in VC I got the 4 first bytes wrong!!.. im completily sure they are stored in the right order but they don't appear correctly in my variable. Here's what im using to read the file: - it used to work in the old bc 3.1 -

FILE *fd;
char s[26];

if ((fd=fopen("c:\\cheerio.map","rt"))==NULL)
{
return 0; // errro opening file
}
fread(&s,sizeof( s ), 1, fd );
fclose( fd);

Lets say my file has:
@ A B C D E F G H I J K L
in binary
64 65 66 67 68 69 70 71 72 73 74 75 76

After reading the file I got:
0 85 64 65 66 67 68 69 70 71 72 73 74

I've tried to read it in binary and text form but it just doesnt work.. Any idea?

Laters and thanks!

"Old programmers don't die,they just terminate and stay resident."
Hi!

Just to let you know that I was able to fix my problem with the VC files, I only had to remove the optimizations of my project and it suddenly worked.. this make wonder if you can't use optimizations at all in your debug version of any project??

Well, by now it's working thanks all and happy holidays!

"Old programmers don't die,they just terminate and stay resident."

This topic is closed to new replies.

Advertisement