Read Two Bytes From Binary File C++

  1. #1

    compile is offline

    Registered User


    read two bytes from a file...

    Hello all,
    i am new to this forum and i am a C beginner... can anyone tell me that how can i read 2 bytes from a file and have that in a single variable...???


  2. #2

    Happy_Reaper is offline

    Fear the Reaper...


    Yeah, you lot use malloc(), fopen() and fread(). Google is now your best friend.

    Teacher: "Yous connect with Internet Explorer, just what is your browser? You know, Yahoo, Webcrawler...?" It's great to encounter the educational system moving in the right direction


  3. #3

    swoopy is offline

    Registered User


    >how can i read two bytes from a file and have that in a unmarried variable...???
    Attempt this.

    Code:

    #include <stdlib.h> #include <stdio.h>  int main(void) {    char filename[] = "file.txt";    char buf[ii];    FILE *fp;        if ((fp = fopen(filename, "rb")) == NULL)    {       printf("Unable to open file: %s\n", filename);       render EXIT_FAILURE;    }        if (fread(buf, 1, 2, fp) == two)    {       printf("%02x %02x\n", (int) buf[0], (int) buf[1]);    }        fclose(fp);    return 0; }


  4. #4

    Kennedy is offline

    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar


    Swoopy, that's just cruel. Y'all know that is far in a higher place the OPs level. . . if the OP turns that in you know it won't be accepted.


  5. #5

    swoopy is offline

    Registered User


    Quote Originally Posted by Kennedy

    Swoopy, that'southward only cruel. You know that is far above the OPs level. . . if the OP turns that in you know it won't be accepted.

    Well the question was so vague, I figured I'd post something to get them started. Without knowing the file contents, or the purpose of the program, one can merely guess (if the purpose of the program is to read the commencement two bytes of a file and that'south information technology, then mission achieved). Bascally I but embellished Happy_Reaper's idea, just without the malloc.


  6. #half-dozen

    Kennedy is offline

    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar


    2 bytes, all the same, can be read into a short, bold of form a 32 bit OS.

    I'd bet that is what the teach was later on. . . or in place of doing a fread() maybe an fgets().


  7. #7

    quzah is offline

    ATH0 quzah's Avatar


    Quote Originally Posted by Kennedy

    Swoopy, that'south but cruel. You know that is far above the OPs level. . . if the OP turns that in yous know it won't be accustomed.

    Y'all must accept dislocated this with the "Here's your Grade-A homework for no effort!" forum.

    Lawmaking:

    unsigned short int information = 0xFF & fgetc ( fp ) << CHAR_BIT; information |= feof( fp ) ? 0 : fgetc( fp ) & 0xFF ; information >>= feof ( fp ) ? CHAR_BIT : 0;
    Expect, it even does error checking!

    Quzah.

    Promise is the commencement pace on the road to disappointment.


  8. #8

    swoopy is offline

    Registered User


    Lightbulb

    Quote Originally Posted by Kennedy

    2 bytes, nevertheless, can be read into a brusque, assuming of course a 32 bit OS.

    I'd bet that is what the teach was afterward. . . or in identify of doing a fread() perchance an fgets().

    Well, that kind of assumes that those two bytes are somehow related, does it not? And it also assumes that those two bytes are a number (maybe they're letters). And if we assume that these two bytes are in fact a number, past using fread to read into a short we are assuming this number was written in binary format and was written on a motorcar with the aforementioned architeture every bit the one nosotros're using.


  9. #ix

    compile is offline

    Registered User


    Last edited by compile; 10-16-2006 at 09:55 PM.


  10. #10

    swoopy is offline

    Registered User


    What yous probably want to practice is create a struct with the header information, and read into that. Bold a brusk is two bytes on your machine:

    Code:

    #include <stdlib.h> #include <stdio.h> typedef struct COFF_HEADER { 	unsigned char byte0; 	curt num_of_sections; 	/* Rest of header info */ };  int principal(void) { 	struct COFF_HEADER header;
    And then read from the file into the struct:

    Code:

                            fread(&header, sizeof(header), ane, fp);
    Or you could declare an unsigned char buffer, and read into that.

    Code:

    #include <stdlib.h> #include <stdio.h>  int main(void) {    char filename[] = "file.txt";    unsigned char buf[1000];    FILE *fp;        if ((fp = fopen(filename, "rb")) == Zippo)    {       printf("Unable to open file: %s\n", filename);       return EXIT_FAILURE;    }        if (fread(buf, 1, 3, fp) == 3)    {       num_of_sections = buf[ii] << eight | buf[i];       /* num_of_sections = buf[one] << 8 | buf[2]; */    }        fclose(fp);    render 0; }
    Last edited by swoopy; 10-xvi-2006 at 11:09 PM.


  11. #xi

    compile is offline

    Registered User



mckoysocidered.blogspot.com

Source: https://cboard.cprogramming.com/c-programming/84251-read-2-bytes-file.html

0 Response to "Read Two Bytes From Binary File C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel