0

so I am trying to read a filesystem disk, which has been provided.

So, what I want to do is read the 1044 byte from the filesystem. What I am currently doing is the following:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);        //Goes to 1024th byte
  int check[sizeof(char)*4];        //creates a buffer array 4 bytes long
  fread(check, 1, 4, fp);           //reads 4 bytes from the file
  printf("%d",check);               //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

The value that check should be printing is 1. However I am getting negative values which keep changing everytime I run the file. I don't understand what I am doing wrong. Am I taking the right approach to reading bytes of the disk, and printing them.

What I basically want to do is read bytes of the disk, and read the values at certain bytes. Those bytes are fields which will help me understand the structure/format of the disk.

Any help would be appreciated. Thank you.

2
  • 1
    Don't you need "rb" instead of "r" for a binary file? Commented May 31, 2011 at 1:11
  • On Windows, yes. On Linux or Unix, no, but probably a good idea anyway for portability (in case the code is later used on Windows). Commented May 31, 2011 at 1:51

2 Answers 2

1

This line:

int check[sizeof(char)*4];

allocates an array of 4 ints.

The type of check is therefore int*, so this line:

printf("%d",check);

prints the address of the array.

What you should do it allocate it as an int:

int check;

and then fread into it:

fread(&check, 1, sizeof(int), fp);

(This code, incidentally, assumes that int is 4 bytes.)

Sign up to request clarification or add additional context in comments.

3 Comments

That should be fread(*check, 1, sizeof(int), fp); & is the dereference operator.
If check is an int, then &check is the address of that int. If check is int* (pointer to an int), then *check is the int at that address.
You're right, I'm being dumb. *sigh... It's been too long a day. I edited my answer below as well.
1
int check[sizeof(char)*4];    //creates a buffer array 4 bytes long

This is incorrect. You are creating an array of four integers, which are typically 32 bits each, and then when you printf("%d",check) you are printing the address of that array, which will probably change every time you run the program. I think what you want is this:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);         //Goes to 1024th byte
  int check;                         //creates a buffer array the size of one integer
  fread(&check, 1, sizeof(int), fp); //reads an integer (presumably 1) from the file
  printf("%d",check);                //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

Note that instead of declaring an array of integers, you are declaring just one. Also note the change from fread(check, ...) to fread(&check, ...). The first parameter to fread is the address of the buffer (in this case, a single integer) into which you want to read the data.

Keep in mind that while integers are probably 32 bits long, this isn't guaranteed. Also, in most operating systems, integers are stored with the least significant byte first on the disk, so you will only read 1 if the data on the disk looks like this at byte 1044:

0x01 0x00 0x00 0x00

If it is the other way around, 0x00 00 00 01, that will be read as 16777216 (0x01000000).

If you want to read more than one integer, you can use an array as follows:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);         //Goes to 1024th byte
  int check[10];                     //creates a buffer of ten integers
  fread(check, 10, sizeof(int), fp); //reads 10 integers into the array
  for (int i = 0; i < 10; i++)
    printf("%d ", check[i]);         //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

In this case, check (without brackets) is a pointer to the array, which is why I've changed the fread back to fread(check, ...).

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.