LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need help understanding the system call 'write' (http://www.linuxquestions.org/questions/programming-9/need-help-understanding-the-system-call-write-863976/)

spaceboy909 02-21-2011 01:01 AM

Need help understanding the system call 'write'
 
Hi all. I'm starting to learn Linux programming and my first bit of code using a system call is not responding the way I expected it to.

It's the write() call. Here's the code:
Code:

#include <unistd.h>
#include <stdlib.h>

int main()
{
        if ((write(1, "Here is some data.......\n", 18)) != 18)
        write(2, "A write error has occurred on file descriptor 1\n",48);

        exit(0);
}

The code seems simple enough. The evaluation of "!=18" should force the following statement to execute if the count is not exactly 18. But that doesn't happen when I alter the string to a shorter or longer sentence.

The statement following the IF evaluation never executes, no matter what. The first string will truncate if the string is longer than the count, and if the string is shorter than the count, the string will print, and then the letter 'A' and a space is printed as a prefix to the next command line prompt.

So why isn't the function acting as expected? Any help is appreciated!

spaceboy909 02-21-2011 01:07 AM

Aw, geez. I figured it out 30 seconds after I posted! I forgot to change the count parameter to match the adjusted string length! Now it's working! :)


All times are GMT -5. The time now is 10:24 AM.