1

So this code functions by itself, but when I use it with my main program, it's somehow pulling in some, what seems to be, completely unrelated parts of the code and writing it to the file I'm writing... All compiles without issues, so I know it's not a definition problem or a pointer issue, and everything else writes properly, but somehow I'm getting over 3200 bytes of stuff that the struct doesn't have any relation to, and writing it at address 0x1 in the file, which isn't any part of the struct either...

struct a {
    unsigned long addr; //File address
    int sz; //Num Bytes
    unsigned long pos; // Buffer Address
};

// Many more than this, but you get the general struct idea..
struct a as[][3] = {
    {{ 0xF245, 5, 0x6F02C4 }},
    {{ 0x471D, 128, 0x65892 }},
    {{ 0x6198F, 12, 0xA4092 }}
}

//Failing code
        fdin = fopen(files[FIRSTFILE]->filename, "rb");

        fdout = fopen(files[SECONDFILE]->filename, "r+b");

        if (!fdin) {
            fprintf(stderr, "Unable to open %s\n", files[FIRSTFILE]->filename);
            fclose(fdin);
            cleanup(ONSCREEN);
            return EXIT_FAILURE;
        }

        if (!fdout) {
            fprintf(stderr, "Unable to open %s\n", files[SECONDFILE]->filename);
            fclose(fdout);
            fclose(fdin);
            cleanup(ONSCREEN);
            return EXIT_FAILURE;
        }

I have other code here, but none that read from a file and write to another like this, But somewhere in here it's writing at least 3200 bytes incorrectly in the range address 0x1-0xC88 in the file and pulling in data that I'm using in popen functions before all of this.

        for (int i = 0; i <= (sizeof(buffer) / sizeof(buffer[0])); i++) {

            memset(buffer, 0, sizeof(buffer));
            fseek(fdin, as[i]->pos, SEEK_SET);
            fread(buffer, 1, as[i]->sz, fdin);

            fseek(fdout, as[i]->addr, SEEK_SET);
            fwrite(buffer, 1, as[i]->sz, fdout);
        }

        if(fclose(fdout)==EOF || fclose(fdin)==EOF) {
            logit(ONSCREEN, "Error closing files.\n\n");
            cleanup(ONSCREEN);
            return EXIT_FAILURE;
        }
        fflush(fdin);
        fflush(fdout);

Here's a piece of the code from the main program that somehow it's pulling information from:

        sleep(1);
        memset(command, 0x00, 256);
        sprintf(command, "./somecommand");
        fp = popen(command, "r");
        if (fp == NULL) {
            logit(ONSCREEN, "popen failed.");
            cleanup(ONSCREEN);
            return EXIT_FAILURE;
        }

        while(fgets(store, sizeof(store), fp)) {
            if (strstr(store, "Expected Output")) {
            break;
            }
        }
        pclose(fp);
        fflush(fp);

Again, all of these function just fine by themselves, but when put together in a single function, they don't play well together... The files (FILE *fp, *fdin, *fdout) are differently named, and the store character array is named differently than buffer. What have I done wrong here?

Seems to be something unsafe about using popen and fopen in the same function like that or something I'm not clearing out properly here...?

4

1 回答 1

0

在您的示例中,as[]有 3 个元素(您的实际代码可能有不同的数字)

// Many more than this, but you get the general struct idea..
struct a as[][3] = {
    {{ 0xF245, 5, 0x6F02C4 }},
    {{ 0x471D, 128, 0x65892 }},
    {{ 0x6198F, 12, 0xA4092 }}
}

但是,您正在使用其中的元素数量buffer(在评论中您说要char buffer[256]对其进行索引:

   for (int i = 0; i <= (sizeof(buffer) / sizeof(buffer[0])); i++) {

        memset(buffer, 0, sizeof(buffer));
        fseek(fdin, as[i]->pos, SEEK_SET);
        fread(buffer, 1, as[i]->sz, fdin);

        fseek(fdout, as[i]->addr, SEEK_SET);
        fwrite(buffer, 1, as[i]->sz, fdout);
    }

将循环更改for为(另请注意,测试从更改<=<):

  for (int i = 0; i < (sizeof(as) / sizeof(as[0])); i++)  

最后 - 我认为你as无缘无故地使用二维数组使事情变得不必要地更复杂(并且可能是错误的)。尝试:

struct a as[] = {
    { 0xF245, 5, 0x6F02C4 },
    { 0x471D, 128, 0x65892 },
    { 0x6198F, 12, 0xA4092 }
}


// ...
   for (int i = 0; i < (sizeof(as) / sizeof(as[0])); i++) {

        memset(buffer, 0, sizeof(buffer));
        fseek(fdin, as[i].pos, SEEK_SET);
        fread(buffer, 1, as[i].sz, fdin);

        fseek(fdout, as[i].addr, SEEK_SET);
        fwrite(buffer, 1, as[i].sz, fdout);
    }
于 2013-03-10T19:44:59.607 回答