parsing a file error

M

Medvedev

when i try to read the first byte from an EXE file it's give different
results
code:
FILE *me,*u;
char buf_me[1];
char buf_u[1];

me= fopen(argv[0],"rb");
u= fopen(argv[1],"rb+");

fread(buf_me,sizeof(char),1,me);
fread(buf_u,sizeof(char),1,u);

printf("%s\n",buf_me);
printf("%s\n",buf_u);

----
when you run this code and put EXE file as an argument , the program
will print
M
MM

why it double the byte in the second time
 
D

Dand

Medvedev said:
when i try to read the first byte from an EXE file it's give different
results
code:
FILE *me,*u;
char buf_me[1];
char buf_u[1];

me= fopen(argv[0],"rb");
u= fopen(argv[1],"rb+");

fread(buf_me,sizeof(char),1,me);
fread(buf_u,sizeof(char),1,u);

printf("%s\n",buf_me);
printf("%s\n",buf_u);

You are not terminating your strings with nulls. Make your buf's 2 bytes and
set the second byte to \0.
Also you should be checking the return values of fopen. If you are opening
the same file with both calls, most likly the second fopen call will be
failing. On many systems you won't be able to open argv[0] successfully at
all. You should also check your return values of fread. Also you are using
%s to print data that is probably going to be binary, not printable text
characters.
 
R

rahul

If you are opening
the same file with both calls, most likly the second fopen call will be
failing. On many systems you won't be able to open argv[0] successfully at
all.

Both of them seem to work under Linux/gcc.
 
C

CBFalconer

Robbie said:
"Medvedev" boggled my mind thusly:
.... snip ...


Best not to put strings of hyphens in usenet posts; it can
confuse some software into thinking it's a signature separator.

No problem. A sig marker is precisely "-- ", with no extra chars.
 
J

jaysome

To be precise, a sig separator is '\n-- \n'

To be precise, in Standard C, which is the topic of this newsgroup, a
sig separator is:

"\n-- \n"

/* start of sig */
#include <stdio.h>
#define SIG_SEPARATOR "\n-- \n"
int main(void)
{
printf("%sjay", SIG_SEPARATOR);
return 0;
}
/* end of sig */
 

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

Similar Threads

parsing error 1
How can I view / open / render / display a pdf file with c code? 0
Command Line Arguments 0
error 28
URGENT 1
A process take input from /proc/<pid>/fd/0, but won't process it 0
Fibonacci 0
C pipe 1

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top