This relates to something else I was doing, but anyway, basically I have this program which has a 'bug' in it. To use the program you must first compile and run it with the variable "readwrite" set to 0 for it to create a data file. Then change it to 1 and recompile it and run it.
Basically the program creates some data and reads it back in again and it should be the same, however the data is wrong (shifted).
Can anyone eplain why?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <errno.h>
#define TRUE 1
#define FALSE 0
#define MINUS -1
#define POSITIVE 0
#define MAXLIST 8000
FILE *ptr1, *ptr2, *ptr3, *ptr4;
FILE *dataptr;
typedef struct {
char name[11];
float stack;
float dolstack;
float tempval;
float crap;
int gamesplayed;
int tookpart;
int tookpart3;
int tookpart5;
int tookpart8;
int five;
int eight;
int three;
int unmatched;
int pfr;
int afr;
int atr;
int arr;
float win;
float win5;
float tempstack;
float crap2;
} PLYR;
PLYR pname[MAXLIST];
int readwrite=1;
int lc=0;
main(argc,argv)
int argc;
char *argv[];
{
if (!readwrite){
while(lc<200){
strcpy(pname[lc].name,"hellothere");
pname[lc].stack=12.4;
pname[lc].dolstack=123.4;
pname[lc].tempval=23.43;
pname[lc].gamesplayed=66;
pname[lc].tookpart=5432;
pname[lc].tookpart3=624;
pname[lc].tookpart5=62222;
pname[lc].five=3536;
pname[lc].eight=994;
pname[lc].three=6365;
pname[lc].unmatched=653;
lc++;
}
datasave();
} else {
lc=0;
dataread();
while ( lc<200) {
printf("\n<%s>", pname[lc].name);
printf("<%f>", pname[lc].stack);
printf("<%f>", pname[lc].dolstack);
printf("<%f>", pname[lc].tempval);
printf("<%d>", pname[lc].gamesplayed);
printf("<%d>", pname[lc].tookpart);
printf("<%d>", pname[lc].tookpart3);
printf("<%d>", pname[lc].tookpart5);
printf("<%d>", pname[lc].five);
printf("<%d>", pname[lc].eight);
printf("<%d>", pname[lc].three);
printf("<%d>", pname[lc].unmatched);
lc++;
}
}
}
datasave(){
int i;
printf("\n openinig %d", errno);
if ( (dataptr=fopen("data.doc", "w+" )) != NULL) {
fwrite( pname, sizeof( pname ), 1, dataptr);
fclose(dataptr);
}
else {
printf("\n error %d", errno);
puts("\nCant creat datafile");
exit(2);
}
printf("\n error after %d", errno);
}
dataread(){
if ( (dataptr=fopen("data.doc", "r" )) != NULL) {
fread( pname, sizeof( pname ), 1, dataptr);
fclose(dataptr);
}
else {
printf("\n read errorerror %d", errno);
puts("\nCant read datafile");
exit(2);
}
}
The output
Code:
<hellothere><12.400000><123.400002><23.430000><66><5432><624><62222><-503316272><-587202557><-1929379816><2>
<ellothere><-202663520.000000><0.000000><0.000000><939524096><1879048213><234881026><243><65142784><417136640><42795008><0>
<llothere><-107612720.000000><406691939335599541850000000000.000000><0.000000><355991552><40894464><-217186304><0><254464><1629440><167168><0>
<lothere><-2076936724493001671600000000000000.000000><-0.003672><0.000000><1390592><159744><15928832><0><994><6365><653><0>
<othere><123.400002><23.430000><0.000000><5432><624><62222><0><-587202557><-1929379816><2><0>
<there><0.000000><0.000000><32.000000><1879048213><234881026><243><-805306368><417136640><42795008><0><0>
<here><406691939335599541850000000000.000000><0.000000><0.000000><40894464><-217186304><0><13631488><1629440><167168><0><0>
<ere><-0.003672><0.000000><0.000000><159744><15928832><0><53248><6365><653><0><0>
<re><23.430000><0.000000><0.000000><624><62222><0><-503316272><-1929379816><2><0><0>
<e><0.000000><32.000000><0.000031><234881026><243><-805306368><65142784><42795008><0><0><0>
<><0.000000><0.000000><0.000000><-217186304><0><13631488><254464><167168><0><0><0>
<><0.000000><0.000000><0.000000><15928832><0><53248><994><653><0><0><0>
<ffFA迒蠛小翠><0.000000><0.000000><0.000000><62222><0><-503316272><-587202557><2><0><0><0>
<fFA迒蠛小翠><32.000000><0.000031><158456721707313236230000000000.000000><243><-805306368><65142784><417136640><0><0><0><0>
<FA迒蠛小翠><0.000000><0.000000><0.000000><0><13631488><254464><1629440><0><0><0><0>
<A迒蠛小翠><0.000000><0.000000><0.000000><0><53248><994><6365><0><0><0><0>
As you can see the data is being shifted to the left (well the "hellothere" string is anyway so I assume the rest is. So do you get the same results and can you explain why this happes?