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...???
Printable View
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...???
Yeah, you use malloc(), fopen() and fread(). Google is now your best friend.
>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;
}
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.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().
You must have confused this with the "Here's your Grade-A homework for no effort!" forum.Quote:
Originally Posted by Kennedy
Look, it even does error checking!Code:unsigned short int data =
0xFF & fgetc ( fp ) <<
CHAR_BIT; data |= feof(
fp ) ? 0 : fgetc( fp )
& 0xFF ; data >>= feof
( fp ) ? CHAR_BIT : 0;
Quzah.
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.Quote:
Originally Posted by Kennedy
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.....
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:
Then read from the file into the struct: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;
Or you could declare an unsigned char buffer, and read into that.Code:fread(&header, sizeof(header), 1, 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")) == 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;
}
oh.. superb.. thanks a lot swoopy... i will come back if i have some more doubts...
thanks again.. :) :)