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
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!
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
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!
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
For example in the below code that I used for testing the itoa() function
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!
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:
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():
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 ;-)
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 !
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:
And I have a linux problem. When I compile my code from a linux terminal I use the command
and I get the warning
Do you have any idea why is this?
Last edited by Scott; 01-12-2010 at 06:30 PM..
Reason: Homework violation. Thread closed.
(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)
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)
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)
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)
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)
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)
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)
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)
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)