473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

parsing a file error

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
Jul 1 '08 #1
7 1598

"Medvedev" <3D********@gmail.comwrote in message
news:7b**********************************@27g2000h sf.googlegroups.com...
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.
Jul 1 '08 #2
On Jul 1, 12:37 pm, "Dand" <f...@email.netwrote:
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.
Jul 2 '08 #3

"Medvedev" boggled my mind thusly:
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);

----
Best not to put strings of hyphens in usenet posts; it can
confuse some software into thinking it's a signature separator.
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
A string with n bytes needs a char array with at least n+1 bytes,
with the first unused byte set to 0.

Google("null-terminated string").

But if you only have 1 character, why use strings at all? Use
characters:

FILE *me = NULL;
FILE *u = NULL;
char buf_me = 0;
char buf_u = 0;

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

fread ( &buf_me, 1, 1, me );
fread ( &buf_u, 1, 1, u );

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

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Jul 2 '08 #4
Robbie Hatley wrote:
"Medvedev" boggled my mind thusly:
.... snip ...
>
> printf("%s\n",buf_me);
printf("%s\n",buf_u);

----

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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 3 '08 #5
CBFalconer said:
Robbie Hatley wrote:
>"Medvedev" boggled my mind thusly:
... snip ...
>>
>> printf("%s\n",buf_me);
printf("%s\n",buf_u);

----

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.
If you think that we can rely on newsreaders to get this right and not be
confused into misreading a hyphen-string ---- as a signature separator,
presumably you also agree that we can rely on compilers to get
initialisation right?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 3 '08 #6
CBFalconer wrote:
Robbie Hatley wrote:
>"Medvedev" boggled my mind thusly:
... snip ...
>>
>> printf("%s\n",buf_me);
printf("%s\n",buf_u);

----

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.
To be precise, a sig separator is '\n-- \n'

Jul 3 '08 #7
On Thu, 03 Jul 2008 12:44:19 +0530, santosh <sa*********@gmail.com>
wrote:
>CBFalconer wrote:
>Robbie Hatley wrote:
>>"Medvedev" boggled my mind thusly:
... snip ...
>>>
printf("%s\n",buf_me);
printf("%s\n",buf_u);

----

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.

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 */
Jul 3 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Willem Ligtenberg | last post by:
I decided to use SAX to parse my xml file. But the parser crashes on: File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError raise exception...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
2
by: Brett | last post by:
I uploaded an ASP.NET website from my developer machine to a remote server. The paths differ slightly as follows: Local Path: C:\Inetpub\wwwroot\mysite Server Path: C:\Inetpub\mysite.com\ I...
0
by: Rosetta | last post by:
I need help with Xml parsing error. I have data that I want to combine with a .pdf file in Adobe Acrobat 7.0 and when I try it gives me this error: Xml parsing error: xml processing instruction...
3
by: =?ISO-8859-1?Q?Fabian_L=F3pez?= | last post by:
Hi, I am parsing an XML file that includes chineses characters, like ^ ÔuÔuà¢à¢²ÅÊDZw.¼ššìéLï³²ÅÊÇÛ or ¥Ø¥¢¥¢¥¤¥í¥ó... The problem is that I get an error like: UnicodeEncodeerror:'charmap' codec...
0
by: =?Utf-8?B?VWxmIFRob3JzZW4=?= | last post by:
I use Visual Studio 2005 for a C-project using an external compiler, and came up with the idea that error parsing would be neat, i.e. enabling the functionality available for a "normal" build...
3
by: spittinfire | last post by:
-------------------------------------------------------------------------------- I am trying to correct the errors on an assignment that has already been graded because I have to now modify it and...
3
by: =?Utf-8?B?RGFuYQ==?= | last post by:
I am re-posting this message after registering my posting alias. When I specify an end tag for the clear element of namespaces in my web.config file, the parser error "Unrecognized element 'add'"...
3
by: GazK | last post by:
I have been using an xml parsing script to parse a number of rss feeds and return relevant results to a database. The script has worked well for a couple of years, despite having very crude...
2
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.