I can't read the first byte of an EXE file!!!!!

M

Medvedev

i write this code to read the first byte from my EXE and compare it
with the entered EXE file
the byte read from both files supposed to be the same but that's don't
happened!!
i tried also with different EXE's but it also failed to read the first
byte which is "M"
--
here is the code:

FILE *a,*b;
int z;
char str1[2];
char str2[2];

a= fopen(argv[0],"rb"); //try to change both file with
another EXE's and u will also fail
b= fopen(argv[1],"rb+");

z= fread(str1,1,1,a);
if (z !=1)
printf("-101");

z= fread(str2,1,1,b);
if (z !=1)
printf("-102");

if(str1 == str2)
{
fwrite("a",1,1,b); // i want to change the first byte
with this char
printf("OK");
}else{
fwrite(str1,1,1,b);
printf("NO");
}
fclose(a);
fclose(b);
 
G

Goedson Paixao

Medvedev escreveu:
i write this code to read the first byte from my EXE and compare it
with the entered EXE file
the byte read from both files supposed to be the same but that's don't
happened!!
i tried also with different EXE's but it also failed to read the first
byte which is "M"
--
here is the code:
char str1[2];
char str2[2];
if(str1 == str2)

Here you're comparing the two pointers str1 and str2 (which will never
be equal). What you want to do is

if (str1[0] == str2[0])

to compare the read bytes.
 
M

Medvedev

Medvedev escreveu:
i write this code to read the first byte from my EXE and compare it
with the entered EXE file
the byte read from both files supposed to be the same but that's don't
happened!!
i tried also with different EXE's but it also failed to read the first
byte which is "M"
--
here is the code:
char str1[2];
char str2[2];
if(str1 == str2)

Here you're comparing the two pointers str1 and str2 (which will never
be equal). What you want to do is

if (str1[0] == str2[0])

to compare the read bytes.

it works but it don't configure the EXE as i called
fwrite("a",1,1,b);
why?!
 
A

Adem24

Medvedev said:
Medvedev escreveu:
i write this code to read the first byte from my EXE and compare it
with the entered EXE file
the byte read from both files supposed to be the same but that's don't
happened!!
i tried also with different EXE's but it also failed to read the first
byte which is "M"
--
here is the code:
char str1[2];
char str2[2];
if(str1 == str2)

Here you're comparing the two pointers str1 and str2 (which will never
be equal). What you want to do is

if (str1[0] == str2[0])

to compare the read bytes.

it works but it don't configure the EXE as i called
fwrite("a",1,1,b);
why?!

Check the position of the file pointer... :)
And of course the file opening mode... :)
 
G

Goedson Paixao

Medvedev escreveu:
i write this code to read the first byte from my EXE and compare it
with the entered EXE file
the byte read from both files supposed to be the same but that's don't
happened!!
i tried also with different EXE's but it also failed to read the first
byte which is "M"
--
here is the code:
    char str1[2];
    char str2[2];
    if(str1 == str2)
Here you're comparing the two pointers str1 and str2 (which will never
be equal). What you want to do is
if (str1[0] == str2[0])
to compare the read bytes.

it works but it don't configure the EXE as i called
fwrite("a",1,1,b);
why?!

In order to write the first byte of the file, you first need to move
back to that position (you've already read it before). Doing this:

fseek(b, 0L, SEEK_SET);

before the fwrite, should do the trick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top