How to output single character in C with write?
Solution 1
You have found your function, all you need now is to pass it proper parameters:
int handle = open("myfile.bin", O_WRONLY);
//... You need to check the result here
int count = write(handle, "b", 1); // Pass a single character
if (count == 1) {
printf("Success!");
}
I did indeed want to use stdout. How do I write a version to display the whole alphabet?
You could use a pre-defined constant for stdout. It is called STDOUT_FILENO
.
If you would like to write out the whole alphabet, you could do it like this:
for (char c = 'A' ; c <= 'Z' ; c++) {
write(STDOUT_FILENO, &c, 1);
}
Solution 2
Let's see the man page of write()
, which says,
ssize_t write(int fd, const void *buf, size_t count);
Description
write()
writes up tocount
bytes from the buffer pointedbuf
to the file referred to by the file descriptorfd
.
As per your requirement, you need to pass an address of a buffer containing b
to print to standard output.
Let's see some code along with, shall we?
#include <stdio.h>
#include <unistd.h>
int main(void) {
char b = 'b';
write(STDOUT_FILENO, &b, 1);
return 0;
}
Let me explain. Here, the STDOUT_FILENO
is the file descriptior for standard output as defined in unistd.h
, &b
is the address of the buffer containing 'b'
and the number of bytes is 1.
Related videos on Youtube
Comments
-
ButterMyBitter about 2 years
Could someone instruct me on how to print a single letter, for example "b" in C while using only the write function (not printf).
I'm pretty sure it uses
#include <unistd.h>
Could you also tell me how the write properties work? I don't really understand
int write( int handle, void *buffer, int nbyte );
Could some of you guys toss in a few C beginner tips as well?
I am using UNIX.
-
Sergey Kalinichenko almost 9 yearsThere is no need to apologize for your question.
-
Weather Vane almost 9 yearsThe
buffer
would be the address of thechar myb ='b'
and thenbyte
size would be 1.
-
-
Sourav Ghosh almost 9 yearsno doubt, but just because the mention of
printf()
, i thought OP wanted to print to stdout, not to file. In that case, it would have beenfprintf()
. Please correct me if i'm wrong. -
Sergey Kalinichenko almost 9 years@SouravGhosh Now that you have mentioned it, I am not sure if OP really wants to write to stdout or not.
-
ButterMyBitter almost 9 yearsI did indeed want to use stdout. The answer above worked for me while this one did not, however. Question - how would I do the write version to display the whole alphabet? I know char c and such would have to do with it. Also, what's "c++;" used for in this and most scenarios?
-
ButterMyBitter almost 9 yearsCool! What is the c++ part used for? I can't find it anywhere. Also, any way to do it without for and maybe replaced by a while?
-
Sergey Kalinichenko almost 9 years@ButterMyBitter This is an increment operation on the character. Rewriting a
for
loop as awhile
loop is a good exercise that is purely mechanical. It's good to try to do independently. -
ButterMyBitter almost 9 yearsThanks for the help. Much appreciated.
-
0___________ almost 3 years
write(STDOUT_FILENO, (char[]){'b'}, 1);
-
Zibri almost 3 years@0___________ he wanted it in a variable :P