|
Home > Archive > Unix Programming > September 2004 > weirdness
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Jani Yusef 2004-09-23, 5: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));
}
| |
|
| 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
| |
| Wayne C. Morris 2004-09-23, 5: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.
| |
| Jani Yusef 2004-09-24, 2: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, and
> 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, so
> 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.
| |
| Fletcher Glenn 2004-09-24, 5: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
| |
| Daniel Rakel 2004-09-24, 5: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'?
|
|
|
|
|