www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/08/00:52:30

From: Martin Ambuhl <mambuhl AT earthlink DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Reading floats from a file
Date: Wed, 07 Apr 1999 16:00:51 -0400
References: <7eflba$qs9$1 AT plug DOT news DOT pipex DOT net>
X-Posted-Path-Was: not-for-mail
X-Accept-Language: en
X-ELN-Date: 7 Apr 1999 20:00:01 GMT
X-ELN-Insert-Date: Wed Apr 7 13:05:22 1999
Organization: Nocturnal Aviation
Lines: 57
Mime-Version: 1.0
NNTP-Posting-Host: 1cust130.tnt12.nyc3.da.uu.net
Message-ID: <370BB973.564DE999@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

Lee wrote:
> 
> Has anyone managed to read a series of floats from a file using ascii reads.
> I always get 0 returned when using fscanf. Has anyone else had this problem.


#include <stdio.h>
#include <stdlib.h>

void BOMB(const char *s)
{
    fprintf(stderr, "BUMMER- %s\n", s);
    exit(EXIT_FAILURE);
}
int main(void)
{
    FILE *f;
    float x;
    size_t i;
    /* Try to write a bunch of floats to a file (echo to stdout) */
    if (!(f = fopen("testit.dat", "w")))
        BOMB("couldn't open testit.dat for output");
    for (i = 0; i < 10; i++) {
        if (!(fprintf(f, "%12f", (x = rand() / (1. + RAND_MAX)))))
            BOMB("fprintf didn't write anything");
        else
            printf("%lu: %f\n", (unsigned long)i, x);
    }
    fprintf(f, "\n");
    fclose(f);
    /* Try to read a bunch of floats from a file (echo to stdout) */
    if (!(f = fopen("testit.dat", "r")))
        BOMB("couldn't open testit.dat for input");
    for (i = 0; i < 10; i++) {
        if (!(fscanf(f, "%12f", &x)))
            BOMB("fscanf didn't convert anything");
        else
            printf("%lu: %f\n", (unsigned long)i, x);
    }
    fclose(f);
    return 0;
}



> 
> Email me privately if possible:)

Why do you think no one else might have this problem?

> 
> leef AT pronexus DOT co DOT uk


-- 
Martin Ambuhl (mambuhl AT earthlink DOT net)
Note: mambuhl AT tiac DOT net will soon be inactive

- Raw text -


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