Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:14:18+00:00 2026-05-27T21:14:18+00:00

I am trying to read a file file.raw and 4 bytes at a time

  • 0

I am trying to read a file “file.raw” and 4 bytes at a time to an array and check if it has the particular 4 byte signature I am looking for. I am having trouble with this. The value of result I get is 0, instead of 4 when using fread.

#include<stdint.h> 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef uint8_t BYTE; 

int main(void)
{
    size_t result;
    FILE *inptr = fopen("file.raw","r+");
    //Check if file can be opened.
    if (inptr == NULL)
    {
        printf("File Open Error\n");
        return -1;
    }
    long int x = 0;

    while(!feof(inptr))
    {
        // Make space for reading in to an array
        BYTE *array = (BYTE *) malloc(10);
        if(array == NULL)
        {
            printf("Array Initialization Error\n");
            return -1;
        }

        result = fread(array,1,4,inptr);
        //Exit if file not read. ** This is where I can't get past.
        if(result != 4)
        {
            printf("File Read Error\n");
            printf("%d\n",result);
            free(array);
            fclose(inptr);
            return -1;
        }

        //Compare strings

        if(memcmp(array,"0xffd8ffe0",4)==0)
        {
            printf("File Start found\n");
            printf("Exiting...\n");
            printf("%p\n",inptr);
            free(array);
            fclose(inptr);
            return 0;
        }
        x++;
        free(array);
    }

    printf("%p\n",inptr);
    printf("%ld\n",x);
    fclose(inptr);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:14:19+00:00Added an answer on May 27, 2026 at 9:14 pm

    My guess is that it doesn’t fail on the first iteration of the while loop, but rather keeps reading the file until you reach end of the file, at which point fread() returns 0 and your program exits.

    The reason it’s not finding the signature is this:

    memcmp(array,"0xffd8ffe0",4)==0
    

    That memcmp() call is almost certainly not what you want (it’s looking for the sequence of ASCII characters '0', 'x', 'f' and 'f').

    PS As noted by @Mat in the comments, for maximum portability you should open the file in binary mode ("r+b" instead of "r+").

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read the .wav file and give the raw data as
I'm trying to read from a file called quiz_questions.txt in my res/raw folder. The
I'm trying to read a file to produce a DOM Document, but the file
I am trying to read from file: The file is multiline and basically i
I am trying to read a file and the error i get is java.io.FileNotFoundException:
I'm trying to read a file of regexes, looping over them and filtering them
I'm trying to read a file line by line to a string type variable
I'm trying to read a file from a local filesystem. I do not have
I'm trying to read in file content, ex : public void myMethod(){ FileInputStream fstream
I'm trying to read a .doc file into a database so that I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.