Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

C structs & fread

Name: Anonymous 2007-11-23 2:52

I have a struct i want to read in from file. To be specific its a windows bmp. It works when i read in the values individually, but when I try and read the structure as a whole it gets the wrong values:


struct BITMAPFILEHEADER{
    unsigned short bfType;        // size: 2
    unsigned int bfSize;        // size: 4
    unsigned short bfReserved1;    // size: 2 
    unsigned short bfReserved2;    // size: 2 
    unsigned int bfOffBits;    //size: 4
};

// works
fread (&fileheader.bfType,2,1,imgFile);
fread (&fileheader.bfSize,4,1,imgFile);
fread (&fileheader.bfReserved1,2,1,imgFile);
fread (&fileheader.bfReserved2,2,1,imgFile);
fread (&fileheader.bfOffBits,4,1,imgFile);

// does not work right
fread (&fileheader,sizeof(BITMAPFILEHEADER),1,imgFile);

Name: Anonymous 2007-11-23 3:03

Learn Erlang binary syntax.

Name: Anonymous 2007-11-23 3:04

Your struct is not necessarily with size 2 and 4 entries.  For optimization purposes, depending on how the compiler feels like fucking with you, it could 4-byte align each field.  Dunno if it's the problem, but it's a possibility.  Faggot

Name: Anonymous 2007-11-23 3:12

Name: Anonymous 2007-11-23 3:14

>>3
I looked at other code on the internet, and they use the same method

Is there a way to make sure the struct gets set the right way? COCKSUCKER

Name: Anonymous 2007-11-23 3:18

>>4
how would i use that?

Name: Anonymous 2007-11-23 3:33

>>1
either turn off forced alingment or expand your struct with 2 unused bytes, dumbass

Name: Anonymous 2007-11-23 3:38

>>6
got it, thanks alot >>4 =D

Name: Anonymous 2007-11-23 5:25

ONE WORD, THE FORCED ALIGNMENT OF DATA

Name: Anonymous 2007-11-23 5:34

Also don't forget your byte swapping.

Name: Anonymous 2007-11-23 8:16

fread (&fileheader.bfType,2,1,imgFile);
fread (&fileheader.bfSize,4,1,imgFile);
fread (&fileheader.bfReserved1,2,1,imgFile);
fread (&fileheader.bfReserved2,2,1,imgFile);
fread (&fileheader.bfOffBits,4,1,imgFile);

Learn to use the sizeof operator you stupid fuck instead of doing some elitist faggotry and directly writing the size.

Name: Anonymous 2007-11-23 8:21

1. Do not depend on specific sizes of int, shorts etc. Use stdint.h for that.
2. Do not read shit into a struct. The only guarantee about that is that you will get the same back if you wrote the struct with the exact same binary. Read shit into an array of u8_t's and extract the values from that.
3. Do not depend on the endianness of the computer. Use a macro to get shit by shifting the u8_t's (you can ifdef the macro to use type punning if appropriate).
4. Never believe something on the Internet without doublechecking.

Name: Anonymous 2007-11-23 8:52

>>11
Uh, no. Element sizes in a file are determined by the format, not the target runtime.

Name: Anonymous 2007-11-23 15:39

>>13
Uh, uh, uh.

Name: Anonymous 2007-11-23 15:51

Uh considered harmful

Name: Anonymous 2007-11-23 19:49

considered harmful considered harmful

Name: Anonymous 2007-11-23 20:48

>>1-16
Who in fucking hell would deal with a language with SHIT like this in dealing with binary formats?  Holy fuck, let C die already.

Name: Anonymous 2007-11-23 23:11

>>17
SPOILERS: Your non-SHIT language is written in C

Name: Anonymous 2007-11-23 23:14

>>18
A language is not written in anything.

Name: Anonymous 2007-11-23 23:26

>>19
Python is "written" in C.

Name: Anonymous 2007-11-23 23:43

>>19
Absolutely correct.

SPOILERS: Your language's compiler/interpreter/linker/assembler/whateverthefuckmakesitwork is written in C

Name: Anonymous 2007-11-23 23:48

>>20
No, CPython is written in C.  CPython is an implementation of python. 

Name: Anonymous 2007-11-24 1:00

>>21
It's all compiled to machine code, fuckface.

Plus, there are plenty of languages with compilers written in the language itself. It's not exactly a novel concept anymore.

Name: Anonymous 2007-11-24 3:03

>>23
You can almost be sure that at some point, that compiler/interpreter/whatthefuckingfuckever passed through C. Even if it was just a temporary bootstrapping-type deal.

You can tune a filesystem, but you can't convince motherfuckers that C is fucking superior.

Name: Anonymous 2007-11-24 3:06

>>12
fatal error C1083: Cannot open include file: 'stdint.h': No such file or directory

so much for the C99 standard library.

Name: Anonymous 2007-11-24 4:25

>>24
can't convince motherfuckers that C is fucking superior.

Because it isn't.  It's like saying iron and carbon are superior to steel, which is fucking stupid.  Core building blocks are a pain in the ass to work with, and are completely unnecessary once you've put them together into something that's actually worth using.

Name: Anonymous 2007-11-24 11:45

>>26
Erlangfag agrees.  Hell, aren't there decent binary handling libs for C where you can give it a simple spec?  Oh wait, you can't just "simply" give compound data around in C without declaring every bit, which doesn't even map to bits anyway, hence OP's problem...

Fucking C.

Name: Anonymous 2007-11-24 14:17

Name: Anonymous 2007-11-24 14:42

>>28
looks like an implementation of python to me

Name: Anonymous 2007-11-24 14:48

>>28
Fix more memory allocation issues found with failmalloc.

Name: Anonymous 2007-11-24 15:18

Toy languages are awesome, that's why pretty much every program you use on your PC is written in a toy language.

And by that I mean nobody uses toy languages. Ever.

Name: Anonymous 2007-11-24 16:58

>>31
Exactly. As much as everybody here wants to bitch and moan about how much C sucks, it doesn't matter.

Name: Anonymous 2007-11-24 16:59

>>31
darcs?

Name: Anonymous 2007-11-24 17:07

>>33
Toy program for toy projects written in toy languages.

Name: Anonymous 2007-11-24 17:31

>>31 Lol everything popular is good.

When has that ever been true?

Name: Anonymous 2007-11-24 17:37

>>35
Not saying that C is better, just that it's what's used.

Name: Anonymous 2007-11-24 17:48

>>36
It's used predominantly because it is better, and proven to be better.

Name: Anonymous 2007-11-24 18:02

it's used predominantly in low level stuff because it is better for low level stuff and proven to be better for low level stuff.
fix'd

Name: Anonymous 2007-11-24 18:04

it's used predominantly because the first thing a new programmer hears is "lol lern2c".
refixéd

Name: Anonymous 2007-11-24 18:09

>>38
agreed

Name: Anonymous 2007-11-24 18:11

>>39
``learn to see''?

Name: ​​​​​​​​​​ 2010-10-25 12:17

Name: Anonymous 2011-01-31 20:44

<-- check em dubz

Name: Anonymous 2011-02-03 5:50

Name: Anonymous 2013-09-01 16:58


The momentum of set theory was such that debate on the paradoxes did not lead to its abandonment. The work of Zermelo in 1908 and Abraham Fraenkel in 1922 resulted in the set of axioms ZFC, which became the most commonly used set of axioms for set theory. The work of analysts such as Henri Lebesgue demonstrated the great mathematical utility of set theory, which has since become woven into the fabric of modern mathematics. Set theory is commonly used as a foundational system, although in some areas category theory is thought to be a preferred foundation.

Name: Anonymous 2013-09-01 19:15


From this definition, it is clear that a set is a subset of itself; for cases where one wishes to rule out this, the term proper subset is defined. A is called a proper subset of B if and only if A is a subset of B, but B is not a subset of A.

Name: Anonymous 2013-09-01 20:14



   /i
  .| |
  | |
 .| |
 | |    __ノ ̄i. __,,,.........,,,__
 | |    ) ヽ、_ゝ-        `ヽ、
 | |    `y'"        `ヽ、 ヽ,
 .| |.    イ / / ⌒/ i  i⌒', ',   ',
 .| |    Lハ__L/ i__ハ__ハ___i .l   i
  | |.     ノ iT!'j   <  ト-┘  |
  .i i     ´7〈""  r─,  ""|   ノ  ..:::::::...
  l l__   く__,iゝ、_ ヽノ  ,.イ_ノ_i^i  ::   ::::::
 ⊂ ̄_ゝ、    `´_'ヽ'ハヘT´_/ヽ-ハ,.ト-、 ::  ::::::
  `! (、 i、  ,γ´ ヘ (ハ)´〒 /(つ   〉:  :::::
   iミiヽ、   ̄)  /  Y 。  i _`T ´|  :::::
   .lミl  ` ̄ ^ーイ  ハ ゚  .ハ"7/  i  :::
   

Don't change these.
Name: Email:
Entire Thread Thread List