Thread: read 2 bytes from a file...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    read 2 bytes from a file...

    Hi 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
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Yeah, you use malloc(), fopen() and fread(). Google is now your best friend.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >how can i read 2 bytes from a file and have that in a single variable...???
    Try this.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
       char filename[] = "file.txt";
       char buf[2];
       FILE *fp;
       
       if ((fp = fopen(filename, "rb")) == NULL)
       {
          printf("Unable to open file: %s\n", filename);
          return EXIT_FAILURE;
       }
       
       if (fread(buf, 1, 2, fp) == 2)
       {
          printf("%02x %02x\n", (int) buf[0], (int) buf[1]);
       }
       
       fclose(fp);
       return 0;
    }

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Swoopy, that's just cruel. You know that is far above the OPs level. . . if the OP turns that in you know it won't be accepted.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Kennedy
    Swoopy, that's just 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 only guess (if the purpose of the program is to read the first two bytes of a file and that's it, then mission accomplished). Bascally I just embellished Happy_Reaper's idea, but without the malloc.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Two bytes, however, can be read into a short, assuming of course a 32 bit OS.

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

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Kennedy
    Swoopy, that's just cruel. You know that is far above the OPs level. . . if the OP turns that in you know it won't be accepted.
    You must have confused this with the "Here's your Grade-A homework for no effort!" forum.
    Code:
    unsigned short int data =
    0xFF & fgetc ( fp ) <<
    CHAR_BIT; data |= feof(
    fp ) ? 0 : fgetc( fp )
    & 0xFF ; data >>= feof
    ( fp ) ? CHAR_BIT : 0;
    Look, it even does error checking!


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Lightbulb

    Quote Originally Posted by Kennedy
    Two bytes, however, can be read into a short, assuming of course a 32 bit OS.

    I'd bet that is what the teach was after. . . or in place of doing a fread() maybe 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, by using fread to read into a short we are assuming this number was written in binary format and was written on a machine with the same architeture as the one we're using.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    ohh.. ohh.... thats really great friends.... i am really in the right place to learn C... thanks a lot for all the replies..... i will make my problem more specific....i have a file format say COFF(common object file format)...i have a varialble declared in my function say num_of_sections,,, now i want the 2nd and 3rd byte of the header(which gives the no of sections in the file) to be in the varialbe num_of_sections ????? please help me...
    thanks all.....
    Last edited by compile; 10-16-2006 at 09:55 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What you probably want to do is create a struct with the header information, and read into that. Assuming a short is two bytes on your machine:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    typedef struct COFF_HEADER
    {
    	unsigned char byte0;
    	short num_of_sections;
    	/* Rest of header info */
    };
    
    int main(void)
    {
    	struct COFF_HEADER header;
    Then read from the file into the struct:
    Code:
    	fread(&header, sizeof(header), 1, 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")) == NULL)
       {
          printf("Unable to open file: %s\n", filename);
          return EXIT_FAILURE;
       }
       
       if (fread(buf, 1, 3, fp) == 3)
       {
          num_of_sections = buf[2] << 8 | buf[1];
          /* num_of_sections = buf[1] << 8 | buf[2]; */
       }
       
       fclose(fp);
       return 0;
    }
    Last edited by swoopy; 10-16-2006 at 11:09 PM.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    oh.. superb.. thanks a lot swoopy... i will come back if i have some more doubts...
    thanks again..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 01:46 PM
  3. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM