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...
所以这段代码本身就起作用,但是当我将它与我的主程序一起使用时,它会以某种方式拉入一些,似乎是完全不相关的代码部分并将其写入我正在编写的文件中...所有编译都没有问题,所以我知道它不是定义问题或指针问题,其他一切写得正确,但不知怎的,我得到超过3200字节的东西,结构与没有任何关系,并写在地址0x1 in该文件,不是结构的任何部分......
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.
我这里有其他代码,但是没有从文件中读取并写入另一个这样的代码,但是在这里的某个地方,它在文件的地址0x1-0xC88范围内写错了至少3200个字节,并提取我正在使用的数据在所有这一切之前的popen功能。
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?
同样,所有这些功能本身都很好,但是当它们放在一个单独的函数中时,它们不能很好地一起播放......文件(FILE * fp,* fdin,* fdout)的名称不同,而商店字符数组的名称与缓冲区不同。我在这做错了什么?
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...?
似乎是关于使用popen和fopen在同一个函数中不安全的东西,或者我在这里没有正确清理的东西...?
1 个解决方案
#1
0
In your example, as[]
has 3 elements (your real-life code may have a different number)
在你的例子中,因为[]有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 }}
}
However, you're using the number of elements in buffer
(which in a comment you say is char buffer[256]
to index it:
但是,你正在使用缓冲区中的元素数量(在评论中你说是char缓冲区[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);
}
Change the for
loop to (note also that the test is changed from <=
to <
):
将for循环更改为(还要注意测试从<=更改为<):
for (int i = 0; i < (sizeof(as) / sizeof(as[0])); i++)
Finally - I think you're making things unnecessarily more complicated (and probably buggy) by using a 2 dimensional array for as
for no reason. try:
最后 - 我认为你通过使用二维数组无缘无故地使事情变得更加复杂(并且可能是错误的)。尝试:
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);
}
#1
0
In your example, as[]
has 3 elements (your real-life code may have a different number)
在你的例子中,因为[]有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 }}
}
However, you're using the number of elements in buffer
(which in a comment you say is char buffer[256]
to index it:
但是,你正在使用缓冲区中的元素数量(在评论中你说是char缓冲区[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);
}
Change the for
loop to (note also that the test is changed from <=
to <
):
将for循环更改为(还要注意测试从<=更改为<):
for (int i = 0; i < (sizeof(as) / sizeof(as[0])); i++)
Finally - I think you're making things unnecessarily more complicated (and probably buggy) by using a 2 dimensional array for as
for no reason. try:
最后 - 我认为你通过使用二维数组无缘无故地使事情变得更加复杂(并且可能是错误的)。尝试:
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);
}