CannibalSmith ([info]cannibalsmith) rakstīja [info]koderi kopienā,
@ 2007-02-22 10:12:00

Previous Entry  Add to memories!  Tell a Friend!  Next Entry
C: fread()
struct stuff { int a; int b; }; Kāpēc sizeof(stuff) > 4 ? Kādus savus sūdus kompilators zagšus bāž manos tipos?

Bet Galvenais jautājums ir, kāpēc sekojošais kods nestrādā? Sākot ar height tiek ielasītas kaut kādas mistiskas vērtības, kas katram kompilatoram ir citas.
#include <stdio.h>

struct BitmapHeader
{
    char type[2];
    unsigned long fileSize;
    unsigned long reserved;
    unsigned long offset;
    unsigned long headerSize;
    unsigned long width;
    unsigned long height;
    unsigned int planes;
    unsigned int bits;
    unsigned long compression;
    unsigned long imageSize;
    unsigned long pixelsPerMeterX;
    unsigned long pixelsPerMeterY;
    unsigned long colorsUsed;
    unsigned long colorsImportant;
};

int main(int argc, char* argv[])
{
    FILE* input,* output;
    struct BitmapHeader bitmap;

    if (argc == 1)
    {
        printf("Usage:\nconvert <input> [<output>]\n");
        return 1;
    }

    if (argc >= 2)
    {
        printf("Opening file %s\n", argv[1]);
        if (!(input = fopen(argv[1], "r")))
        {
            printf("Cannot open input file!\n");
            return 2;
        }
        //fread(&bitmap, sizeof(struct BitmapHeader), 1, input);
        fread(bitmap.type, 1, 2, input);
        fread(&bitmap.fileSize, 4, 1, input);
        fread(&bitmap.reserved, 4, 1, input);
        fread(&bitmap.offset, 4, 1, input);
        fread(&bitmap.headerSize, 4, 1, input);
        fread(&bitmap.width, 4, 1, input);
        fread(&bitmap.height, 4, 1, input);
        fread(&bitmap.planes, 2, 1, input);
        fread(&bitmap.bits, 2, 1, input);
        fread(&bitmap.compression, 4, 1, input);
        fread(&bitmap.imageSize, 4, 1, input);
        fread(&bitmap.pixelsPerMeterX, 4, 1, input);
        fread(&bitmap.pixelsPerMeterY, 4, 1, input);
        fread(&bitmap.colorsUsed, 4, 1, input);
        fread(&bitmap.colorsImportant, 4, 1, input);
        printf("type = %c%c\n", bitmap.type[0], bitmap.type[1]);
        printf("fileSize = %u\n", bitmap.fileSize);
        printf("offset = %u\n", bitmap.offset);
        printf("headerSize = %u\n", bitmap.headerSize);
        printf("width = %u\n", bitmap.width);
        printf("height = %u\n", bitmap.height);
        printf("planes = %u\n", bitmap.planes);
        printf("bits = %u\n", bitmap.bits);
        printf("compression = %u\n", bitmap.compression);
        printf("imageSize = %u\n", bitmap.imageSize);
        printf("pixelsPerMeterX = %u\n", bitmap.pixelsPerMeterX);
        printf("pixelsPerMeterY = %u\n", bitmap.pixelsPerMeterY);
        printf("colorsUsed = %u\n", bitmap.colorsUsed);
        printf("colorsImportant = %u\n", bitmap.colorsImportant);
    }

    if (input)
    {
        fclose(input);
    }
    getchar();
    return 0;
}


(Lasīt komentārus)

Nopūsties:

No:
Lietotājvārds:
Parole:
Ievadi te 'qws' (liidzeklis pret spambotiem):
Temats:
Tematā HTML ir aizliegts
  
Ziņa:

Gandrīz jau aizmirsu pateikt – šis lietotājs ir ieslēdzis IP adrešu noglabāšanu. Operatore Nr. 65.
Neesi iežurnalējies. Iežurnalēties?