Hello. This is my first post here, so please be gentle!
So I want to read 4 bytes (32 bits) at a time from ANY file into a buffer. I am not sure what the best way to do this is, and I am unsure what type my buffer should be.
Once I have this buffer, I need to perform one's complement arithmetic on the elements of the buffer.
So far, here is what I have:
Code:
int main()
{
FILE *fp
u_int32 *buf //using u_int32 for buffer
int num_32bits = 0;
fp = fopen(filename, "rb");
if(fp == NULL)
{
printf("the file could not be opened");
}
else
{
fseek(fp, 0, SEEK_END);
filelen = ftell(fp);
rewind(fp);
buf = (u_int32 *)malloc((filelen + 1) * sizeof(u_int32));
//want to read 32 bits at time, that is, 4 bytes
num_32bits = filelen / 4;
fread(buf, 4, num_32bits, fPointer);
//more code here....
So far, I just have a .txt file with the string: hello, world. That is 12 bytes. When I have it print out to the terminal as chars for debugging purposes, it prints out:
o o h
That means that it is "truncating" everything but the first byte in a 4-byte pattern, So it is printing out hello,world
Shouldn't it be printing out the full hello, world everytime? What am I doing wrong here? How can I ensure that each element in the array has a bit-string of 32 bits (4 bytes)? Any help would be appreciated.
PS. I know nothing about endianness...jsyk