delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/28/19:19:52

From: nine1one AT juno DOT com
To: djgpp AT delorie DOT com
Date: Wed, 28 Jul 1999 06:12:27 -0500
Subject: Problems with file I/O and the value 13 (EOF?)
Message-ID: <19990728.061228.-4108575.0.nine1one@juno.com>
X-Mailer: Juno 2.0.11
X-Juno-Line-Breaks: 0,11-40
X-Juno-Att: 0
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

Greetings,
  I am having problems with reading and writing to disk files with the
standard C I/O libraries. I am writing a program that needs to read and
write values from disk. I have a record of strings and short integers.
Whenever I write the value 13 to a file and read it back I get a large
number, as if it shifted bytes and is reading a value in the upper byte
of the short integer. I got some code online. It writes and reads values
1 -> 10 to 10 records. When I modified it to read and write values 1 ->
15 it did exactly what I feared. It did values 1 through 12 and craped
out on 13 through 15. Below I have enclose the code. I am using DJGPP as
my primary compiler but I ran the code thorugh Borland C++ 4.52 and it
did the same thing. Here is the code:

#include <stdio.h>
                               
/* writes & reads 15 records from the file "junk". */
void main()
{
int i,j;
FILE *fptr;
int r;
                                   
/* create the file of 15 records */
fptr=fopen("junk","w");
for (i=0;i<=15; i++)
{
r=i;
fwrite(&r,sizeof(int),1,fptr);
}
fclose(fptr);
                                   
/* read the 15 records */
fptr=fopen("junk","r");
for (i=0;i<=15; i++)
{
fread(&r,sizeof(int),1,fptr);
printf("%d ",r);
}
fclose(fptr);
}

The output should be "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" but all of the
digits after 12 aren't what they are suppost to be.  I am totally
puzzled.  - Mike Young
___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: http://dl.www.juno.com/dynoget/tagj.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019