Integer shift problem

srivats123
srivats123 used Ask the Experts™
on
Hi All,

   My problem is If I write an Integer into a file, integer shifts beyond a limit. I get junk values like
-828373764 for some value like 25 or 30 written into the file. Any suggestions. Thanks.I've declared the numbers as unsigned long.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Could you post the code?

Author

Commented:
struct page
{
     int pointer;
     int no_entries;
     record rec[7];    
};//end struct page

struct record
{
        int key;
        int order;
}

page p;

for(int i=0;i<=6;i++)
{
  p.rec[i].key = some_int_val;
  p.rec[i].order = some_int_val;
}

fseek(fp,valid_position,0);
fwrite(&p,sizeof(page),1,fp);


Now an fread(&p,sizeof(page),1,fp) returns me some junk
value for key..


 
code is incomplete so it is hard to say, but...
Are you checking file functions' return values ?
Do you close the file, and the open it again, if not, do you set back file pointer to correct place before reading?
I think it is some I/O error.

Regards
Pawel.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Commented:
Have you opened your file as binary file both in writing and in reading?

Author

Commented:
Thanks a million,dude.

Commented:
You are welcome. Don't ask me if I have done this kind of errors myself before cause I have not! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial