无法使用 write() 写入标准输出
Unable use write() to write to standard out
我正在尝试使用 write() 一次将一个字符写入标准输出一个字节。该程序编译并且 运行 很好,但输出结果不太正确。现在我试图通过写入将其写入 运行,但我没有在控制台中获得任何输出。
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);
目前我正在从 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
但我正在尝试使用 write 而不是 fprintf 为每个进程获取此输出:
This is process 2 with ID 27711 and parent id 27710
这可能是相关的,但 fprintf(stderr, %d\n", &s[i])
有效并且 fprintf(stdout, %d\n", &s[i])
?我相信 stderr
获得优先权,但我不认为其他输出根本不会打印?
是的,我做了一些研究,但我发现的大多数答案都使用 fwrite()
或在 C++ 中,因此不是我要找的。
缺少一些细节,但根据您目前发布的内容,这应该可行:
void myfunction(char *s, int fd, int n) {
if( write(fd, s, strlen(s)) < 0) {
perror("Error: Write problems.");
}
wastesometime(n);
}
让我们确保了解这里发生的事情。在 myfunction
中,参数 s
是指向 char
的指针,更具体地说,它指向一个以 null 结尾的字符数组的第一个字符,这是 C 对字符串的定义.
您尝试使用的 write
函数也接受指向 char
的指针。但是,它并不坚持(或期望)指针专门指向以空字符结尾的字符串;它可以是指向任何字符数组的指针。由于 write
不假设一个以 null 结尾的字符串,你必须准确地告诉它你想要它写多少个字符。
由于您是从一个以 null 结尾的字符串开始的,因此您可以通过以下方式计算字符串中的实际字符数(这正是您需要作为第三个参数传递给 write
的数字)调用标准 strlen
函数,如我所示。
您的问题可能与 STDOUT_FILENO
定义不正确有关。请尝试所有这四个调用:
myfunction(buffer, 1, n);
myfunction(buffer, 2, n);
myfunction(buffer, fileno(stdout), n);
printf("STDOUT_FILENO = %d\n", STDOUT_FILENO);
void myfunction(char *s, int fd, int n)
{
for (int i = 0; i < strlen(s); i++)
{
if (write(fd, s + i, 1) < 0)
{
perror("Error: Write problems.");
}
sleep(n);
}
}
你不需要发送 &s 来写,因为 s 已经是一个指针,已经包含了一个地址...发送 &s 就像发送一个地址的地址...另外,不要忘记递增该地址在您的循环中,否则您将一遍又一遍地写相同的字符。
当您使用 printf(或其任何变体)时,您可以使用 %c 来打印一个字符,您可以使用 %s 来打印一个字符串 (char *)
我正在尝试使用 write() 一次将一个字符写入标准输出一个字节。该程序编译并且 运行 很好,但输出结果不太正确。现在我试图通过写入将其写入 运行,但我没有在控制台中获得任何输出。
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);
目前我正在从 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
但我正在尝试使用 write 而不是 fprintf 为每个进程获取此输出:
This is process 2 with ID 27711 and parent id 27710
这可能是相关的,但 fprintf(stderr, %d\n", &s[i])
有效并且 fprintf(stdout, %d\n", &s[i])
?我相信 stderr
获得优先权,但我不认为其他输出根本不会打印?
是的,我做了一些研究,但我发现的大多数答案都使用 fwrite()
或在 C++ 中,因此不是我要找的。
缺少一些细节,但根据您目前发布的内容,这应该可行:
void myfunction(char *s, int fd, int n) {
if( write(fd, s, strlen(s)) < 0) {
perror("Error: Write problems.");
}
wastesometime(n);
}
让我们确保了解这里发生的事情。在 myfunction
中,参数 s
是指向 char
的指针,更具体地说,它指向一个以 null 结尾的字符数组的第一个字符,这是 C 对字符串的定义.
您尝试使用的 write
函数也接受指向 char
的指针。但是,它并不坚持(或期望)指针专门指向以空字符结尾的字符串;它可以是指向任何字符数组的指针。由于 write
不假设一个以 null 结尾的字符串,你必须准确地告诉它你想要它写多少个字符。
由于您是从一个以 null 结尾的字符串开始的,因此您可以通过以下方式计算字符串中的实际字符数(这正是您需要作为第三个参数传递给 write
的数字)调用标准 strlen
函数,如我所示。
您的问题可能与 STDOUT_FILENO
定义不正确有关。请尝试所有这四个调用:
myfunction(buffer, 1, n);
myfunction(buffer, 2, n);
myfunction(buffer, fileno(stdout), n);
printf("STDOUT_FILENO = %d\n", STDOUT_FILENO);
void myfunction(char *s, int fd, int n)
{
for (int i = 0; i < strlen(s); i++)
{
if (write(fd, s + i, 1) < 0)
{
perror("Error: Write problems.");
}
sleep(n);
}
}
你不需要发送 &s 来写,因为 s 已经是一个指针,已经包含了一个地址...发送 &s 就像发送一个地址的地址...另外,不要忘记递增该地址在您的循环中,否则您将一遍又一遍地写相同的字符。
当您使用 printf(或其任何变体)时,您可以使用 %c 来打印一个字符,您可以使用 %s 来打印一个字符串 (char *)