unknown character in output

R

rudra

hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
FILE *stream;
char sys[8],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);
printf("bit=%s",sysptr);
}
and the output is

bit=32�

why the irritating symbol after 32 is coming? can you suggest me
something?
 
W

Walter Roberson

rudra said:
fread(sysptr,1,2,stream);
printf("bit=3D%s",sysptr);

fread does not add a binary 0 after the data read in, so you
are attempting to print a string that is not necessarily NUL terminated.
 
S

santosh

rudra said:
hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){

Implicit int return is no longer legal. The most portable forms of main
are:

int main(void)

and

int main(int, char**)

as they are required to be supported by all conforming implementations.
FILE *stream;
char sys[8],*sysptr;

Walter Roberson has already addressed your problem, but you might want
to format your code before posting again. Unformatted code is difficult
to read and expecting respondants to do the formatting themselves is
rather optimistic, to say the least.
int bit;
sysptr = &sys[0];
sys[6] = 0x0;

If you want to store a null character a more readable altenative might
be:

sys[6] = '\0';
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);

popen and pclose, if you want help on them in future, are topical on
comp.unix.programmer.
 
S

santosh

rudra said:
well, i am lest experienced with C. can you plz show me how i can di
that?

Please quote relevant portions of the post you are responding to. This
makes it easier for people to respond to you.

To manually terminate an array of char with a null character, just
assign one at the desired position. For example if an array contains
seven characters and you want to null terminate them and thus create a
string you can do this:

arr[7] = '\0';

It is equivalent to

arr[7] = 0;

but the former form makes the character nature of 'arr' explicit.

You do not need to add a null character to strings as they are already
so terminated by definition. Almost all the Standard library string
functions recognise and preserve the null terminator. One notable
exception is strncpy for certain values.
 
K

Keith Thompson

rudra said:
hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
FILE *stream;
char sys[8],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);
printf("bit=%s",sysptr);
}
and the output is

bit=32�

why the irritating symbol after 32 is coming? can you suggest me
something?

Please properly indent any code you post here. Run it through the
"indent" program (I recommend "indent -kr"; read the documentation to
see what that means).

Questions about popen and pclose are topical in comp.unix.programmer.

You assign the value 0x0 (which would be better written as the
equivalent '\0', since it's a null character) to sys[6]. Why 6?
Think about how many characters you're reading into your sys array.

The sysptr variable is unnecessary. You could have replaced sysptr
with sys in the calls to fread and printf.

You should always print a new-line character at the end of your
output, unless you have some specific reason not to do so.

"main()" should be "int main(void)", and you should add "return 0;" at
the end of your program.

The string you pass to popen is system-specific, as is the popen
function itself, but I'll mention in passing your program makes some
very specific and probably unwarranted assumptions about the output of
the "file" command. It also performs no error checking (not all
systems have /sbin/init). For details, consult comp.unix.programmer.
 
B

Bill Reid

Keith Thompson said:
rudra said:
hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
FILE *stream;
char sys[8],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);
printf("bit=%s",sysptr);
}
and the output is

bit=32?

why the irritating symbol after 32 is coming? can you suggest me
something?

Please properly indent any code you post here.

Brilliant.

This is what happens when I don't post here for a little
while to keep the "regulars" in line.

The problem is obviously an improperly terminated
string. This has even been pointed out, and yet NOBODY
has managed to provide the simple solution:

sysptr[fread(sysptr,1,2,stream)]='\0';

Although that's not EXACTLY the way I would do it (or
actually do it "in real life"), but you get the idea...you have
to insert the "nul" terminator after the last byte-character
actually read by fread(), and since fread() returns the
number of byte-characters actually read (in this case),
well...there it is...
 
I

Ike Naar

Please properly indent any code you post here.

Good advice.
Run it through the "indent" program (I recommend "indent -kr"; read
the documentation to see what that means).

That does not always work out well.
The "indent" variant that runs on this computer, for instance,
- has no "-kr" option.
- inserts tab characters (at least in default mode), which makes
the output less readable in some newsreaders.

As long as posted code is readable, it should be okay; it does not
have to be formatted according to the rules of some formatting tool.

If you don't like the format, you can always run it through indent yourself,
using your preferred settings.

Ike
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top