LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2011, 01:01 AM   #1
spaceboy909
LQ Newbie
 
Registered: Mar 2008
Location: Missouri, USA
Posts: 23

Rep: Reputation: 0
Arrow 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!
 
Old 02-21-2011, 01:07 AM   #2
spaceboy909
LQ Newbie
 
Registered: Mar 2008
Location: Missouri, USA
Posts: 23

Original Poster
Rep: Reputation: 0
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!
 
  


Reply

Tags
system, write



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use write system call within a system call byteinnovator Linux - Kernel 18 08-05-2010 07:13 AM
modifying write system call linuxdoniv Programming 14 08-08-2008 06:10 AM
how to write my own system call in mandrake 10.2 jawahar_bits Linux - Kernel 2 02-03-2007 10:14 AM
write system call basics.... bisDude Programming 6 11-04-2005 06:48 PM
How to write a system call to display the process information? balasquare@yahoo.com Programming 3 09-30-2005 01:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration