1

I have a rainbow table that I have stored to an HDD. I am reading the file and trying to transfer it to a struct. I have another dump executable that displays all the structs in the rainbow table. TO test the fread() and fseak() I found a struct at a particular index that I want to look at but when I do I get all 0's.

I think I am using fseek wrong.

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct data { unsigned char a[16]; char b[20]; } data; int main(int argc, char **argv){ FILE *f = fopen( *(argv + 1), "rb"); data e = { .a = ""}; fseek(f, 35929 * sizeof(struct data), SEEK_SET); fread(&e, sizeof(struct data), 1, f); printf("%s\n", e.a); return 0; } 
8
  • 3
    Do you really want to read at byte 35929, or do you want to read the 35929th structure? if the latter, then you should fseek(f, 35929 * sizeof(data), SET_SEEK);. Commented Nov 24, 2015 at 5:13
  • Showing some text of your file will help us to find the issue. Commented Nov 24, 2015 at 5:15
  • 2
    fseek(f, 35929, SET_SEEK); I don't think there is any defined constant SET_SEEK maybe you meant to use SEEK_SET . Commented Nov 24, 2015 at 5:17
  • if the printf is showing zeros, then maybe that is what is in the file. How do you know they are zeros when you are printing a string? Commented Nov 24, 2015 at 5:21
  • haccks, the file is a sparse file on the HDD that is about 600MB. I have a working application that prints out valid data from that rainbow table, the issue is when I try to directly acess that section of the file with fseek(). ameyCU you are right, my code used SEEK_SET, a compiler error occurs when using SET_SEEK Mark Lakata I have a working app that lists valid values and their locations of the raibow table. The issue comes when I try to manually seek to that location. Commented Nov 24, 2015 at 5:41

1 Answer 1

3

Your use of fseek shows no immediate indication of why you are reading all zeros at your requested offset within the file. However, there is really no way to tell what the problem may be because there is no validation of the success or failure of any of the critical operations up to that point in your code. It is impossible to tell if the failure is due to a failure to open f or if the file contains a sufficient number of bytes to support the offset your request, or whether fseek or fread succeeded or failed at that offset, etc...

To begin to understand where the problem lies, you must validate each of the necessary operations up to the point of printf. At least then you would have some reasonable idea at which point your code is failing before running it through a debugger. (may not)

A good first step to solving your problem (as well as just proper code validation) is to check the return of each function called to insure it succeeds and to further validate the reasonableness of the values, as needed. An example of the minimum validation required would be similar to:

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct data { unsigned char a[16]; char b[20]; } data; int main(int argc, char **argv){ FILE *f = fopen (argv[1], "rb"); data e = { .a = {0}, .b = {0} }; long int size = 0; if (!f) { fprintf (stderr, "error: file open failed '%s'.\n", argv[1]); return 1; } if (fseek (f, 0, SEEK_END)) { fprintf (stderr, "error: fseek SEEK_END failed\n."); return 1; } if ((size = ftell (f)) == -1){ fprintf (stderr, "error: ftell failed to return size of file\n."); return 1; } rewind (f); if ((unsigned long)size < 35929 * sizeof e) { fprintf (stderr, "error: offset exceeds file size '%ld'\n.", size); return 1; } if (fseek (f, 35929 * sizeof e, SEEK_SET)) { fprintf (stderr, "error: fseek SEEK_SET failed\n."); return 1; } if (!fread (&e, sizeof e, 1, f)) { fprintf (stderr, "error: fread failed to read data into 'e'\n."); return 1; } printf("%s\n", e.a); fclose (f); return 0; } 

Give it a try and report back with additional information and everyone here is happy to lend any additional help you may require.

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

2 Comments

Like I mentioned above I simplified my code to shorten it and keep it simple based around the fseek() and fread() but I do have validation that the file was successfully opened or not. The issue ended up being not opening the correct input file for the rainbow table. My makefile was not outputting the table. The result was that the code was opening the executable file that made the table. I didn't make the Makefile so I assumed that it worked, I even looked over it quickly and it looked correct, it was missing some commands which resulted in the error I had.
Your example does indeed have some safety features I wouldn't have thought of. I will have to save that for future reference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.