Thread: Read 32 Bits From Any File Into Buffer

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    2

    Read 32 Bits From Any File Into Buffer

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose that the number of bytes in the file is not an exact multiple of 4. What do you plan to do with the last few bytes? Currently, from what I see with your calculation of filelen, you probably want to discard the leftover bytes since num_32bits / 4 would truncate. Therefore, you may not need to calculate filelen or num_32bits in the first place, e.g.,
    Code:
    // ...
    
    int main(void)
    {
        u_int32 buf;
        FILE *fp = fopen(filename, "rb");
    
        if (fp == NULL)
        {
             printf("the file could not be opened");
        }
        else
        {
            while (fread(&buf, sizeof(buf), 1, fp) == 1)
            {
                // work with buf
                // ...
            }
            fclose(fp);
        }
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    2
    You are correct, I have not accounted for the event in which there aren't exactly 4 bytes at the end of the file. I hadn't gotten to that part yet, but basically I plan on padding the last byte with 0's. I think I am going to keep filelen for sure since I will end up needing it for other methods, but I can discard num_32bits.

    Can you explain why you used
    Code:
    u_int32 buf;
    and not
    Code:
    u_int32 *buf;
    ?

    Don't I want to pass the bytes to a pointer?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by fuzzymanboy
    Don't I want to pass the bytes to a pointer?
    Yes, hence the use of &buf. Declaring buf to be a pointer doesn't make sense since a buffer is essentially space to hold something, and a pointer doesn't hold anything except an address, but you only need an address because you need the address of the buffer to pass to fread.

    In your scenario, you would be using malloc and free, but calling malloc to allocate a single integer is rather silly when you can just declare the integer. Worst still, if you end up calling malloc and free in the loop, you would be reallocating memory on each iteration, and memory allocation is relatively expensive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 05-02-2017, 05:28 PM
  2. using C read() into buffer issues
    By npc93 in forum C Programming
    Replies: 5
    Last Post: 02-12-2017, 02:13 PM
  3. dump the buffer read from binary file
    By r00t in forum C Programming
    Replies: 23
    Last Post: 11-23-2009, 11:51 PM
  4. Bitwise Operations to Read ON/OFF Bits
    By pseudonoma in forum C Programming
    Replies: 4
    Last Post: 02-25-2008, 03:15 PM
  5. read bits (newbie)
    By Varg Vikernes in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2005, 02:07 PM

Tags for this Thread