delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/02/03:01:53

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problems with fread and fwrite
Date: Wed, 01 Oct 1997 09:38:12 -0700
Organization: Alcyone Systems
Lines: 49
Message-ID: <34327C74.12C26F0D@alcyone.com>
References: <60tlf0$7d3$1 AT loki DOT cf DOT ac DOT uk>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Emyr James wrote:

> I am trying to get djgpp to do a raw memory dump to a file. I wrote
> the following
> small program to output 1000 double float variables. The file produced
> should be
> exactly 8000 bytes long. However, the program produces files with
> lengths that vary
> each time but are typically 8020 bytes long. Needless to say,
> replacing the fwrite
> to an fread to read in the file produces garbage. What is going on ??
> Is there another way of doing it ?

When you get back written results that aren't what you expect, it
usually means that the stream is opened in text mode, not binary mode.

Instead of 

    file = fopen("filename", "w");

use 

    file = fopen("filename", "wb");

This is a standard ANSI C question, and doesn't have specifically to do
with DJGPP.

> I tried the program with gcc 2.6.3 on a Sun workstation and that works
> ok (i.e
> output files are 8000 bytes long and can be read in to give meaningful
> values).
> HELP!!!!

This is consistent with the problem I describe above, because on Unix
systems the logical newline (LF) is the same as the physical newline
(LF), whereas on DOS systems the physical newline is CR LF.

If you intend to be reading and writing binary data, not text, then
always open the file in binary mode, or else you'll be guaranteed to run
into problems on systems where the logical and physical newlines differ
(e.g., DOS, Windows, Macintosh).

-- 
          Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                        Alcyone Systems / http://www.alcyone.com/max/
   San Jose, California, United States / icbm://+37.20.07/-121.53.38
                                      \
   "After each war there is a little / less democracy to save."
                                    / Brooks Atkinson

- Raw text -


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