Read Two Bytes From Binary File C++
-
10-16-2006 #1
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...???
-
10-16-2006 #2
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
-
10-16-2006 #3
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; }
-
10-sixteen-2006 #4
{Jaxom,Imriel,Liam}'s Dad
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.
-
10-xvi-2006 #5
Registered User
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.
Originally Posted by Kennedy
-
x-xvi-2006 #half-dozen
{Jaxom,Imriel,Liam}'s Dad
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().
-
10-16-2006 #7
ATH0
Y'all must accept dislocated this with the "Here's your Grade-A homework for no effort!" forum.
Originally Posted by Kennedy Expect, it even does error checking!Lawmaking:
unsigned short int information = 0xFF & fgetc ( fp ) << CHAR_BIT; information |= feof( fp ) ? 0 : fgetc( fp ) & 0xFF ; information >>= feof ( fp ) ? CHAR_BIT : 0;
Quzah.
Promise is the commencement pace on the road to disappointment.
-
x-16-2006 #8
Registered User
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.
Originally Posted by Kennedy
-
10-16-2006 #ix
Registered User
Last edited by compile; 10-16-2006 at 09:55 PM.
-
ten-xvi-2006 #10
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:
And then read from the file into the struct: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;
Or you could declare an unsigned char buffer, and read into that.Code:
fread(&header, sizeof(header), ane, fp);
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.
-
x-17-2006 #xi
Registered User
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