Need help with fopen/fread prob in W98 - PLEASE 
Author Message
 Need help with fopen/fread prob in W98 - PLEASE

I have a 3.6 MB binary file that I am trying to open with fopen and read
with fread.  This is just a quick a dirty tool, so I didn't want to use MFC
and all the baggage that goes with it.  The problem is this:  the file opens
fine, but I start getting bad reads ( as indicated by the return value from
fread() ) before I reach the end of the file.  I am pretty sure it is
writing ok ( I checked the returns values to the fwrite() function as I
wrote the file ).  Any ideas?  Is there a limit to the file size that
fopen/fread will work on in W98?  I need help quickly.

Mark



Sun, 14 Apr 2002 03:00:00 GMT  
 Need help with fopen/fread prob in W98 - PLEASE
Are you opening the file as binary?

e.g. fopen("myfile.bin", "rb")  or fopen("myfile.bin", "wb").

Constantin


Quote:
> I have a 3.6 MB binary file that I am trying to open with fopen and read
> with fread.  This is just a quick a dirty tool, so I didn't want to use
MFC
> and all the baggage that goes with it.  The problem is this:  the file
opens
> fine, but I start getting bad reads ( as indicated by the return value
from
> fread() ) before I reach the end of the file.  I am pretty sure it is
> writing ok ( I checked the returns values to the fwrite() function as I
> wrote the file ).  Any ideas?  Is there a limit to the file size that
> fopen/fread will work on in W98?  I need help quickly.

> Mark



Sun, 14 Apr 2002 03:00:00 GMT  
 Need help with fopen/fread prob in W98 - PLEASE
I used FILE *fp = fopen( "filename", "w" ) to open the file, and
write( &floatVar, sizeof( float ), 1, fp ) to write it and fread( &floatVar,
sizeof( float ), 1, fp ) to read it.  The first 36 or so floating point
values are read correctly, but then it starts failing reads (i.e. returns 0
from fread() ).


Quote:
> Are you opening the file as binary?

> e.g. fopen("myfile.bin", "rb")  or fopen("myfile.bin", "wb").

> Constantin



> > I have a 3.6 MB binary file that I am trying to open with fopen and read
> > with fread.  This is just a quick a dirty tool, so I didn't want to use
> MFC
> > and all the baggage that goes with it.  The problem is this:  the file
> opens
> > fine, but I start getting bad reads ( as indicated by the return value
> from
> > fread() ) before I reach the end of the file.  I am pretty sure it is
> > writing ok ( I checked the returns values to the fwrite() function as I
> > wrote the file ).  Any ideas?  Is there a limit to the file size that
> > fopen/fread will work on in W98?  I need help quickly.

> > Mark



Sun, 14 Apr 2002 03:00:00 GMT  
 Need help with fopen/fread prob in W98 - PLEASE
I believe that by default fopen(...,"w") opens the file in text (translated)
mode.
That's why you're seeing errors.

Quote from MSDN on fopen():
-------------------------------------
[...]
If t or b is not given in mode, the default translation mode is defined by
the global variable _fmode. If t or b is prefixed to the argument, the
function fails and returns NULL.

Quote from MSDN on _fmode:
----------------------------------
_fmode
The _fmode variable sets the default file-translation mode for text or
binary translation. It is declared in STDLIB.H as

extern int _fmode;

The default setting of _fmode is _O_TEXT for text-mode translation.
_O_BINARY is the setting for binary mode.

You can change the value of _fmode in either of two ways:

Link with BINMODE.OBJ. This changes the initial setting of _fmode to
_O_BINARY, causing all files except stdin, stdout, and stderr to be opened
in binary mode.

Change the value of _fmode directly by setting it in your program

End
Quote-----------------------------------------------------------------------
---------

Constantin


Quote:
> I used FILE *fp = fopen( "filename", "w" ) to open the file, and
> write( &floatVar, sizeof( float ), 1, fp ) to write it and
read( &floatVar,
> sizeof( float ), 1, fp ) to read it.  The first 36 or so floating point
> values are read correctly, but then it starts failing reads (i.e. returns
0
> from fread() ).



Sun, 14 Apr 2002 03:00:00 GMT  
 Need help with fopen/fread prob in W98 - PLEASE

Quote:

>I used FILE *fp = fopen( "filename", "w" ) to open the file, and
>write( &floatVar, sizeof( float ), 1, fp ) to write it and fread( &floatVar,
>sizeof( float ), 1, fp ) to read it.  The first 36 or so floating point
>values are read correctly, but then it starts failing reads (i.e. returns 0
>from fread() ).

FILE *fp = fopen( "filename", "wb" ) ;

The 'b' ensures that the file is written as binary. (For example, a byte
that looks like a newline does *not* get turned into a CR/LF.) Make sure
you also add the 'b' mode to the fopen for reading.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



Sun, 14 Apr 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Question about fopen/fread on W98

2. help with fopen() and fread()

3. fopen & fread BASIC

4. fopen() fseek() fread() fwrite()

5. using fopen, fread, getc, putc, etc in Windows 95

6. fopen, fwrite, fread, fclose

7. Do fopen and fread, etc. effect errno

8. Size limit of fopen,fread ?

9. prob:with fopen() & fflush()

10. Please Help (fread, fwrite)

11. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

12. Please Help (fread, fwrite)

 

 
Powered by phpBB® Forum Software