weirdness
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > weirdness




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    weirdness  
Jani Yusef


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-04 10:54 PM

When I compile and executethe code below I se ethis weidness in the
output. For example,
$ ./a.out 2 3
puke
puke
üÿ¿Xüÿ¿Æ-

Where are those weird characters coming from? This is on a Debian 3
box but I see the same thing on Solaris, for what that's worth.


#define MAXBUFFER 7
int main(int argc, char *argv[]){

char input[MAXBUFFER ];//create a character array to hold
input from the
user
char output[MAXBUFFER ];//create a character array to hold
output
int depth=atoi(argv[2]);
numberOfProcesses=atoi(argv[1]);

read(0,input,MAXBUFFER );
sprintf(output,"%s",input);
write(1,output,strlen(output));
}





[ Post a follow-up to this message ]



    Re: weirdness  
manu


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-04 10:54 PM

This is not weirdness. You are doing it wrong.

The reason is that your input buffer is not a string, because there  is
no null terminator in the buffer. So in the input buffer, after puke
you have '\n' that you gave at the input and rest of the characters are
uninitialized. After that this whole buffer is copied to output by
sprintf and printed at the stdout by write.
Cheers,
-Manu
----------------
Manu Garg
http://manugarg.freezope.org






[ Post a follow-up to this message ]



    Re: weirdness  
Wayne C. Morris


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-04 10:54 PM

In article <d3be1825.0409231206.5d120ed8@posting.google.com>,
jani@persian.com (Jani Yusef) wrote:

> When I compile and executethe code below I se ethis weidness in the
> output. For example,
> $ ./a.out 2 3
> puke
> puke
> üÿ¿Xüÿ¿Æ-
>
> Where are those weird characters coming from? This is on a Debian 3
> box but I see the same thing on Solaris, for what that's worth.
>
>
> #define MAXBUFFER 7
> int main(int argc, char *argv[]){
>
>         char input[MAXBUFFER ];//create a character array to hold
> input from the
> user
>         char output[MAXBUFFER ];//create a character array to hold
> output
>         int depth=atoi(argv[2]);
>         numberOfProcesses=atoi(argv[1]);
>
>         read(0,input,MAXBUFFER );

read() doesn't always fill the buffer, and may even return an error.  You
have to check the return value to determine whether there was an error, and
if not, how many characters it put into the buffer.

>         sprintf(output,"%s",input);

You're using input as if it were a zero-terminated string, but read()
doen't zero-terminate it.  You have to either add a zero byte after the
last byte read, or specify the string's length via the "*" format spec.

sprintf() will zero-terminate the data it writes into the output buffer, so
that buffer needs to be big enough for that extra byte.  Likewise for the
input buffer if you decide to zero-terminate it.





[ Post a follow-up to this message ]



    Re: weirdness  
Jani Yusef


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-24-04 07:50 AM

Wayne C. Morris wrote:
> In article <d3be1825.0409231206.5d120ed8@posting.google.com>,
>  jani@persian.com (Jani Yusef) wrote:
>
> 
>
>
> read() doesn't always fill the buffer, and may even return an error.  You
> have to check the return value to determine whether there was an error, an
d
> if not, how many characters it put into the buffer.
>
> 
>
>
> You're using input as if it were a zero-terminated string, but read()
> doen't zero-terminate it.  You have to either add a zero byte after the
> last byte read, or specify the string's length via the "*" format spec.
>
> sprintf() will zero-terminate the data it writes into the output buffer, s
o
> that buffer needs to be big enough for that extra byte.  Likewise for the
> input buffer if you decide to zero-terminate it.

Thanks!! A little googling based on the responses led me to make use of
bzero(). I added the line bzero(input,MAXBUFFER); to the above code and
now all is well.






[ Post a follow-up to this message ]



    Re: weirdness  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-24-04 10:51 PM



Jani Yusef wrote:
> Wayne C. Morris wrote:
> 
>
>
> Thanks!! A little googling based on the responses led me to make use of
> bzero(). I added the line bzero(input,MAXBUFFER); to the above code and
> now all is well.
>
bzero() is strictly BSD.  If you want portability, use memset().

--

Fletcher Glenn






[ Post a follow-up to this message ]



    Re: weirdness  
Daniel Rakel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-24-04 10:51 PM

Jani Yusef wrote:

> Thanks!! A little googling based on the responses led me to make use of
> bzero(). I added the line bzero(input,MAXBUFFER); to the above code and
> now all is well.

Have you tried to type in a longer word, say 'puking'?





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:52 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register