简体   繁体   English

使用fread()和fseek()

[英]working with fread() and fseek()

I have a rainbow table that I have stored to an HDD. 我有一个已存储到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. 我还有另一个转储可执行文件,显示了Rainbow表中的所有结构。 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. 为了测试fread()和fseak(),我在要查看的特定索引中找到了一个结构,但是当我得到全0时。

I think I am using fseek wrong. 我认为我使用fseek错误。

#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;
}

Your use of fseek shows no immediate indication of why you are reading all zeros at your requested offset within the file. 使用fseek不能立即表明为什么要读取文件中请求的偏移量的所有零。 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... 无法判断失败是由于无法打开 f 导致的,还是文件是否包含足以支持您的偏移量的字节,或者fseekfread在该偏移量处是否成功或失败,等等。

To begin to understand where the problem lies, you must validate each of the necessary operations up to the point of printf . 要开始了解问题所在,必须验证直到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. 请尝试一下,并提供其他信息,并进行报告,这里的每个人都很乐意为您提供任何其他帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM