Problems with the use of write()


 
Thread Tools Search this Thread
Top Forums Programming Problems with the use of write()
# 1  
Old 01-11-2010
Problems with the use of write()

I have written a code in c using eclipse under linux ubuntu 9.10.
I want to print an array of integers in stdout using write(). When I run this command line

Code:
write(1, &readbuffer, sizeof(readbuffer) /sizeof(int));

What I get is "�Lw�薨��" !
From what I know the second argument of write is pointer to data, so normally I shouldn't have problems with the type of data. Why is this happening and is there a way to print my data correctly using write()?
I know that using printf() would make things easier, but I would like to know if there is a solution for using the function write().
Thank you!
# 2  
Old 01-11-2010
Hi,

write() doesn't interpret the data, like printf() does. What happens here: each byte of your integer array are printed out on stdout as it would be a stream of char. Hence the weird result.

If you want to print in human form, you need a formatted string (using e.g. printf, using integer to string convert, building the string yourself etc.).

HTH,
Loïc.
--
My (Unix) blog: removed

Last edited by Scott; 01-11-2010 at 05:46 PM.. Reason: Your blog doesn't add any value to this thread
# 3  
Old 01-12-2010
Thank you Loic for your answer...That means I need something like the atoi() function but doing the opposite, right?!I haven't found a library function doing this, so I suppose I have to do it from the beginning...thank you again for your answer!
# 4  
Old 01-12-2010
See man printf, and read printf examples. Unless you're forbidden to use stdio it really is more efficient than what you're trying to do.
# 5  
Old 01-12-2010
I have overcome my problem by writting a function itoa() that converts numbers to strings...but now I am facing a new challenge with this inconvenient function, that I hate so much Smilie
For example in the below code that I used for testing the itoa() function

Code:
int main()
{
    int s=-5;
    int g=6;
    static char d[10];
    static char h[10];
    itoa(s,d);
    itoa(g,h);
    write(1, d, sizeof(d));
    write(1, h, sizeof(h));
}

when I run it I only get the number -5 and not the 6 as well, which means that my function itoa() works just fine. The problem is obviously in write, which, as it seems, can not be used to print two different things in a raw!
What can I do for this? I don't want to use any command from the printf family!
# 6  
Old 01-12-2010
Quote:
Originally Posted by gkons
I have overcome my problem by writting a function itoa() that converts numbers to strings...but now I am facing a new challenge with this inconvenient function, that I hate so much
Is there a salient reason why you don't want to use printf()? A homework maybe? [1]
itoa() ? Something like:
Code:
#include <stdio.h>
#define itoa(x,s) sprintf(s,"%d",x)

Quote:
Originally Posted by gkons
when I run it I only get the number -5 and not the 6 as well, which means that my function itoa() works just fine. The problem is obviously in write, which, as it seems, can not be used to print two different things in a raw!
Your getting -56, right? Just insert a '\n' between the write():
Code:
    write(1, d, sizeof(d));
    write(1,"\n",1);
    write(1, h, sizeof(h));
    write(1,"\n",1);

Quote:
Originally Posted by gkons
What can I do for this? I don't want to use any command from the printf family!
[1] There are reasons while we might want to write its own printf() version. I just want to make sure, it's one of those ;-)

Cheers,
Loïc.

Last edited by Scott; 01-12-2010 at 05:33 PM..
# 7  
Old 01-12-2010
Actually this is a homework from a friend of mine...and as I have big interest in programming I wanted to help him. Me personally I started programming with cobol and gw basic, then pascal and then went to c and after I learn some things I want, I will proceed to some more oriented programming like java.
I also started getting busy with some linux scripting Smilie !
Anyway in the stdout I was getting only -5 without 6, but the problem was the sizeof. When I used strlen() the result was ok.
My code for the itoa() function is:

Code:
void reverse(char s[])
{
      int c, i, j;
      for (i=0, j=strlen(s)-1; i<j; i++, j--)
      {
            c=s[i];
            s[i]=s[j];
            s[j]=c;
      }
}

void itoa(int n, char s[])
{
     int i, sign;
     if ((sign=n)<0)
            n=-n;
     i=0;
     do 
     {
             s[i++]=n%10+'0';
     }
     while ((n/=10)>0);
     if (sign<0)
           s[i++]='-';
     s[i]='\0';
     reverse(s);
}

And I have a linux problem. When I compile my code from a linux terminal I use the command

Code:
gcc -std=99 -Werror -Wall -pedantic -Wextra -errors my_file.c -o executable

and I get the warning
Code:
/usr/bin/ld: warning: cannot find entry symbol rrors; defaulting to 0000000008048500

Do you have any idea why is this?

Last edited by Scott; 01-12-2010 at 06:30 PM.. Reason: Homework violation. Thread closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?

(Apologies for any typos.) OSX 10.12.3 AND Windows 10. This is for the serious Python experts on at least 3.5.x and above... In script format sys.stdout.write() AND sys.stderr.write() seems to work correctly. Have I found a serious bug in the interactive sys.stdout.write() AND... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Is it possible to write write multiple cronjobs in shellscript??

Hi All, I need the answer of below question? 1) How to write multiple cronjobs in shellscript? Is there any way or we cant write in shellscript... Regards, Priyanka (2 Replies)
Discussion started by: pspriyanka
2 Replies

3. Shell Programming and Scripting

Not getting how to write

Hi Team, I am trying for below requiremebt but not getting how to go. When i give below command amqrfdm -m SUN it gives the output as below amqrfdm for v7.0 Date(2013-08-12 )Time(14.06.42) Version(7.0.1.3)(p701-103-100813) Product(WebSphere MQ for Solaris (SPARC platform)) #)... (12 Replies)
Discussion started by: darling
12 Replies

4. Shell Programming and Scripting

Problems with "write" and "wall"

Hello, I am using VirtualBox to simulate a small network with two Linux computers, the host is Mac OS X. My problem is that I can't send "write" and "wall" messages from the host to one of those Linux computers. Here is what works: - The virtual Linux computer answers "ping" messages that have... (5 Replies)
Discussion started by: 123_abc
5 Replies

5. Programming

Write-Write on a socket

Can anyone tell what happens if each end writes at the same time on the same socket ? - if one of them issues a read() after write() has completed, will it record into the buffer what the other sent ? ex. e1 writes to e2 - - - while - - - e2 writes to e1 (at the same time) e1 read () - what... (1 Reply)
Discussion started by: gendaox
1 Replies

6. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

7. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies

8. Shell Programming and Scripting

how to write

hi buddies i want to write a awk script for the file like this: Aaj rootha huwa ek dost bahut yaad aaya, Achha guzra huwa kuch waqt bahut yaad aaya, Meri aankhon ke har ek ashq pe rone wala, Aaj jab aankh yeh royi to bahut yaad aaya, Jo mere dard ko seene mein chhupa leta thaa, Jo... (2 Replies)
Discussion started by: LAKSHMI NARAYAN
2 Replies

9. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies
Login or Register to Ask a Question