LinuxQuestions.org
Review your favorite Linux distribution.
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 10-17-2005, 05:06 AM   #1
bisDude
LQ Newbie
 
Registered: Oct 2005
Posts: 6

Rep: Reputation: 0
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.......
 
Old 10-17-2005, 05:49 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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));

}
 
Old 10-17-2005, 06:33 AM   #3
bisDude
LQ Newbie
 
Registered: Oct 2005
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 10-17-2005, 08:05 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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.
 
Old 10-17-2005, 08:58 AM   #5
bisDude
LQ Newbie
 
Registered: Oct 2005
Posts: 6

Original Poster
Rep: Reputation: 0
ok thanks for clearing that up for me....
 
Old 11-04-2005, 06:36 PM   #6
acornish
LQ Newbie
 
Registered: Nov 2005
Location: Vancouver, BC
Distribution: Fedora Core 2/4
Posts: 1

Rep: Reputation: 0
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.
 
Old 11-04-2005, 06:48 PM   #7
qais
LQ Newbie
 
Registered: Oct 2005
Location: Canada
Distribution: Fedora Core 4
Posts: 4

Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Retain Server Connection Up, If Client Break, write system call return SIGPIPE anandss2004 Programming 2 10-24-2005 12:16 AM
how to write a simple c prog to call a script RajRed Programming 1 10-05-2005 05:10 PM
How to write a system call to display the process information? balasquare@yahoo.com Programming 3 09-30-2005 01:05 AM
Write sys call failing ppy Programming 1 11-05-2004 08:57 AM
difficulties with large files and system call read/write jwstric2 Programming 2 08-22-2004 11:42 PM


All times are GMT -5. The time now is 05:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration