Difficulty freading an array of structures

This is a discussion on Difficulty freading an array of structures within the C Programming forums, part of the General Programming Boards category; Hi. I typed a file of data directly int a text document, made sure not a single whitespace was present ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Difficulty freading an array of structures

    Hi.

    I typed a file of data directly int a text document, made sure not a single whitespace was present and also made sure no extra newlines were present. I then saved it as a different file extension ".dat" so that I could fread it as a binary file.

    Unfortunately, as I fread the data into structures and sent it to the console I found many errors. It read too many pieces of data into each structure thus leaving garbled garbage at the end of the printout.

    Here are the facts:

    the structure:

    typedef struct atom{

    char elem[25];
    char symb[25];
    int numb;
    float mass;
    float dens;
    float melt;
    float boil;
    float spht;
    };

    I then declared an array of these structures like so:

    atom atom_array[max_num/8];

    ...where "max_num" is the number of lines I counted in the file (no newline spacing is in the file and I printed out "max_num"s value---it was correct)...

    I read it in like this:

    int t;
    for( t=0; t<(max_num/8); t++)
    { fread(&atomo[t], sizeof(atom), 1, fr); }

    ...where "fr" is the "FILE" pointer...

    And printed it out like this:

    for( t=0;t<max_num/8;t++){
    printf("%s \n%s \n%i \n%.2f \n%.2f \n%.2f \n%.2f \n%.2f \n",atomo[t].elem, atomo[t].symb, atomo[t].numb, atomo[t].mass, atomo[t].dens, atomo[t].melt, atomo[t].boil, atomo[t].spht); }

    So what went wrong? Any help is appreciated...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > I then saved it as a different file extension ".dat" so that I could fread it as a binary file.
    You're way off

    Consider the numb member of the structure (an integer)

    If you wanted to type in the number 1234, you would not have the characterers "1234" in the file, but these 4 hex characters "\x00\x00\x04\xD2"
    This being the hex value of the integer 1234 (namely 0x4D2).

    But you might be using a little endian machine, in which case it would need to be "\xD2\x04\x00\x00"

    Floats are unimaginally worse than this

    Start with a regular text file (spaces, newlines etc), and use fscanf to read / convert the text into appropriate binary representations.
    Use fwrite to write the array out, then you will be able to use fread to read it back.

    If your editor is capable of viewing binary files, examine something produced by this method.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Wow, thanks for the correction there!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing array of structures causing bus error
    By Aaron M in forum C Programming
    Replies: 5
    Last Post: 03-05-2006, 01:40 AM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 06:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 10:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 05:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21