Greetings,

I am trying to write a data file using a native C structure which has floats in it. Is this possible? My system is Windows XP with Visual C 6, and ActiveState perl. I am not trying to cross platforms, I just want to transfer from perl float to c float directly into a structure format. I could translate from a text file to the structure in c, but would rather do it in perl.

Enclosed are some sample code. I have tried pack with with "f" and "d", but end up with garbage in the structure.

#write test file open OUTF, ">c\\current\\test.bin"; binmode (OUTF); my $buff = pack "sfff", 99, 1.34, 1.44, 1.55; print OUTF $buff; print OUTF $buff; close OUTF; print "done.\n";
// test reader in visual c 6 typedef struct { short ss; float a,b,c; } TEST_REC; void iitest() { FILE *fd; TEST_REC tester[2]; long rv; fd = fopen("test.bin","r"); rv = fread( tester, sizeof(TEST_REC), 2, fd); printf(" ss %d, %5.3f, %5.3f, %5.3f, \n", tester[0].ss, tester[0]. +a,tester[0].b,tester[0].c); printf(" ss %d, %5.3f, %5.3f, %5.3f, \n", tester[1].ss, tester[1]. +a,tester[1].b,tester[1].c); fclose(fd); exit (0); }

Thanks. Eugene

UPDATE

The padding was the problem on the c side structure (padded to 4 bytes). I was already dealing with the padding elsewhere, but didn't think about it here. DUH! I was too focused on thinking the float was the problem.

I added the short as a sanity check, but since I was already insane...

Thanks.


In reply to transfer float data to c by NatureFocus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.