From: Martin Ambuhl Newsgroups: comp.os.msdos.djgpp Subject: Re: fseek()/ fread() troubles Date: Sun, 02 May 1999 20:33:42 -0400 Content-Transfer-Encoding: 7bit References: <7gikpt$k2i$1 AT nnrp1 DOT dejanews DOT com> X-Posted-Path-Was: not-for-mail X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-ELN-Date: 3 May 1999 00:32:42 GMT X-ELN-Insert-Date: Sun May 2 17:35:19 1999 Organization: Nocturnal Aviation Lines: 40 Mime-Version: 1.0 NNTP-Posting-Host: 1cust124.tnt11.nyc3.da.uu.net Message-ID: <372CEEE6.1B3F3A85@earthlink.net> X-Mailer: Mozilla 4.51 [en] (Win95; I) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com benvg AT hotpop DOT com wrote: > > I've been trying to use fseek and fread to do some file i/o but weird things > are happening. I can't seem to figure it out so I wrote this short program, > and maybe someone can help me out. Kill the magic numbers and call fseek correctly: [ ... ] #if 0 write(fileno(fp), &data, 20); #endif fwrite(&data, sizeof(STRUCTURE), 1, fp); /* mha */ data.number++; } [ ... ] #if 0 fseek_ret = fseek(fp, SEEK_SET, rand_num * 20); #endif fseek_ret = fseek(fp,rand_num * sizeof(STRUCTURE), SEEK_SET); ftell_ret = ftell(fp); #if 0 fread(&data, 20, 1, fp); #endif fread(&data, sizeof(STRUCTURE), 1, fp); [ ... ] /* vi: set cindent ts=4 sw=4 et tw=72: */ -- Martin Ambuhl (mambuhl AT earthlink DOT net) Note: mambuhl AT tiac DOT net will soon be inactive