rudra <bn********@gmail.comwrites:
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.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"