I'm trying to use write() to write a char to standard out one byte at a time. The program compiles and runs fine but the output isn't turning out quite right. Now I'm trying to get it to run with write but I'm not getting any output in the console.
void myfunction(char *s, int fd, int n){
for(int i = 0; i != sizeof(s) - 1; ++i){
fprintf(stderr, "%s\n", &s[i]); // This correctly prints out the string
if( write(fd, &s, 1) < 0){ // This will not print to the console
perror("Error: Write problems."); // Have not gotten any errors yet
}
wastesometime(n); // This just calls a for loop that runs n times
}
}
sizeN = sprintf(buffer, "This is process %d with ID %ld and parent id %ld\n", i, (long)getpid(), (long)getppid());
myfunction(buffer, STDOUT_FILENO, n);
Currently I'm getting output in the form of this from fprintf:
process 2 with ID 27711 and parent id 27710
rocess 2 with ID 27711 and parent id 27710
ocess 2 with ID 27711 and parent id 27710
cess 2 with ID 27711 and parent id 27710
ess 2 with ID 27711 and parent id 27710
ss 2 with ID 27711 and parent id 27710
s 2 with ID 27711 and parent id 27710
2 with ID 27711 and parent id 27710
2 with ID 27711 and parent id 27710
with ID 27711 and parent id 27710
with ID 27711 and parent id 27710
ith ID 27711 and parent id 27710
th ID 27711 and parent id 27710
h ID 27711 and parent id 27710
ID 27711 and parent id 27710
ID 27711 and parent id 27710
D 27711 and parent id 27710
27711 and parent id 27710
27711 and parent id 27710
7711 and parent id 27710
711 and parent id 27710
11 and parent id 27710
1 and parent id 27710
and parent id 27710
and parent id 27710
nd parent id 27710
d parent id 27710
parent id 27710
parent id 27710
arent id 27710
rent id 27710
ent id 27710
nt id 27710
t id 27710
id 27710
id 27710
d 27710
27710
27710
7710
710
10
0
But I am trying to get this output using write instead of fprintf, for every process:
This is process 2 with ID 27711 and parent id 27710
This may be relevant but fprintf(stderr, %d\n", &s[i])
works and fprintf(stdout, %d\n", &s[i])
? I believe stderr
gets priority but I didn't think that the other output wouldn't get printed at all?
Yes, I have done some research but most answers I've found use fwrite()
or are in C++ and are therefore not what I'm looking for.
There are some details missing, but based on what you've posted so far, this ought to work:
void myfunction(char *s, int fd, int n) {
if( write(fd, s, strlen(s)) < 0) {
perror("Error: Write problems.");
}
wastesometime(n);
}
Let's make sure we understand what's going on here. Within myfunction
, the argument s
is a pointer to char
, and more specifically, it points to the first character of a null-terminated array of characters, which is C's definition of a string.
The write
function you're trying to use also accepts a pointer to char
. However, it does not insist (or expect) that the pointer be specifically to a null-terminated string; it can be a pointer to any array of characters. Since write
does not assume a null-terminated string, you must tell it precisely how many characters you want it to write.
Since you're starting with a null-terminated string, you can compute the actual number of characters in the string (which is precisely the number you need to pass as the third argument to write
) by calling the standard strlen
function, as I've shown.
Your problem may have something to do with STDOUT_FILENO
not being defined correctly. Please try all four of these calls:
myfunction(buffer, 1, n);
myfunction(buffer, 2, n);
myfunction(buffer, fileno(stdout), n);
printf("STDOUT_FILENO = %d\n", STDOUT_FILENO);