unknown character in output

Discussion in 'C Programming' started by rudra, Aug 1, 2008.

  1. rudra

    rudra Guest

    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?
     
    rudra, Aug 1, 2008
    #1
    1. Advertising

  2. In article <>,
    rudra <> wrote:

    >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.

    --
    "No sincere artist was ever completely satisfied with his labour."
    -- Walter J. Phillips
     
    Walter Roberson, Aug 1, 2008
    #2
    1. Advertising

  3. rudra

    rudra Guest

    well, i am lest experienced with C. can you plz show me how i can di
    that?
     
    rudra, Aug 1, 2008
    #3
  4. rudra

    santosh Guest

    rudra wrote:

    > 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.

    > printf("bit=%s",sysptr);
    > }
    > and the output is
    >
    > bit=32?
    >
    > why the irritating symbol after 32 is coming? can you suggest me
    > something?
     
    santosh, Aug 1, 2008
    #4
  5. rudra

    santosh Guest

    rudra wrote:

    > 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.
     
    santosh, Aug 1, 2008
    #5
  6. rudra <> writes:
    > 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) <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"
     
    Keith Thompson, Aug 1, 2008
    #6
  7. rudra

    Bill Reid Guest

    Keith Thompson <> wrote in message
    news:...
    > rudra <> writes:
    > > 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...

    ---
    William Ernest Reid
     
    Bill Reid, Aug 1, 2008
    #7
  8. rudra

    Ike Naar Guest

    In article <>,
    Keith Thompson <> wrote:
    >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
     
    Ike Naar, Aug 2, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,851
    Joerg Jooss
    Jan 19, 2006
  2. raavi
    Replies:
    2
    Views:
    914
    raavi
    Mar 2, 2006
  3. David Palau

    Recommended Web Method Output for Unknown Custom Object?

    David Palau, May 25, 2007, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    250
    John Saunders [MVP]
    May 25, 2007
  4. Vincent Arnoux
    Replies:
    1
    Views:
    256
    Arnaud Bergeron
    Aug 11, 2006
  5. Priyank Shah
    Replies:
    3
    Views:
    116
    Brian Candler
    Aug 5, 2010
Loading...

Share This Page