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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-17-2005, 05:06 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2005
Posts: 6
Rep:
|
write system call basics....
hi guys....im new at unix programming and have a question on the write() system call....
how do I write integers?
Code:
int x = 2;
write(1, (const void*)&x, sizeof(x));
it compiles fine...
I just want to write the value 2 to the screen, but nothing is displayed....
its no problems writing a char buffer:
char *str = "hey";
write(1, (const void*)str, strlen(str));
I know I can use other functions to do this, but I need to use system calls for another purpose...just thought I'd try to write a int to the screen first  .....hopefully someone can help me with this...
Thanks a bunch.......
|
|
|
|
10-17-2005, 05:49 AM
|
#2
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
you can't write an 'integer' to the screen, you can only write a string.
which you must null terminate yourself in the normal way.
So you will need to convert the int to an ascii string
representation. itoa or sprintf.
try this:
Code:
int main (int argc, char ** argv, char ** envp)
{
int x = 012;
write(1, (const void*)&x, sizeof(x));
x = 0110;
write(1, (const void*)&x, sizeof(x));
x = 0105;
write(1, (const void*)&x, sizeof(x));
x = 0114;
write(1, (const void*)&x, sizeof(x));
x = 0114;
write(1, (const void*)&x, sizeof(x));
x = 0117;
write(1, (const void*)&x, sizeof(x));
x = 0; /* null terminate */
write(1, (const void*)&x, sizeof(x));
x = 012; /* new line */
write(1, (const void*)&x, sizeof(x));
}
|
|
|
|
10-17-2005, 06:33 AM
|
#3
|
|
LQ Newbie
Registered: Oct 2005
Posts: 6
Original Poster
Rep:
|
thanks for the reply.....
so if I have a int array that I want to write to a pipe...
int x[] = {1, 2, 3};
write(pipe[1], ...., ....);
i suppose I first need to convert the int array to a string array first, Im I right?
|
|
|
|
10-17-2005, 08:05 AM
|
#4
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
No of course not.
If you want to send raw data that's fine.
It's just earlier you were sending to a terminal device
(the screen). That's a different matter because it needs ASCII
codes to print what you expect.
|
|
|
|
10-17-2005, 08:58 AM
|
#5
|
|
LQ Newbie
Registered: Oct 2005
Posts: 6
Original Poster
Rep:
|
ok thanks for clearing that up for me....
|
|
|
|
11-04-2005, 06:36 PM
|
#6
|
|
LQ Newbie
Registered: Nov 2005
Location: Vancouver, BC
Distribution: Fedora Core 2/4
Posts: 1
Rep:
|
Code correction
write( .., &x, sizeof(x) ) will write 4 bytes, 3 of which will be NUL.
This is due to sizeof(x) being 4.
On an Intel architecture machine, the first byte will be non-NUL.
On other architectures, the last byte written might be non-NUL.
int x;
should really be
char x;
Then the sample code would write one byte per system call,
since sizeof(x) is 1.
|
|
|
|
11-04-2005, 06:48 PM
|
#7
|
|
LQ Newbie
Registered: Oct 2005
Location: Canada
Distribution: Fedora Core 4
Posts: 4
Rep:
|
If you need to convert double then you can use gcvt() function .
write(fd,gcvt(double,sizeof(double),buff),sizeof(double));
just do man on gcvt.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting 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
|
|
|
All times are GMT -5. The time now is 06:54 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|