简体   繁体   English

不使用 cout、printf 或 puts() 打印内容

[英]To print something without using cout, printf or puts()

I had learned that我了解到

inline ostream & _Cdecl ostream::operator<< (const signed char * _s) {
    outstr(_s, (const signed char *)0);
    return *this;
}

is how the insertion operator (<<) is declared(overloaded) in the iostream.h header file.是在 iostream.h header 文件中声明(重载)插入运算符 (<<) 的方式。 Can I possibly use the same function to print a string value on screen?我可以使用相同的 function 在屏幕上打印字符串值吗?

I tried我试过了

#include<iostream.h>
int main() {
    outstr("Hello world!", (const signed char *)0);
    return 0;
}

it ended up in error.它最终出错了。 I would like to use something like this in order to see if there is some possible way to answer this query of printing something on screen without using printf, cout or puts().我想使用这样的东西来查看是否有一些可能的方法来回答这个在屏幕上打印某些东西的查询而不使用 printf、cout 或 puts()。

Update: I would welcome if you have any suggestions other than更新:如果您有任何建议,我将不胜感激

#include<stdlib.h>
void main() {
    system("echo /"Hello world!/"");
}

NB: I have no restrictions if you can provide the C equivalent code that can print without a printf(), cout or puts()注意:如果您可以提供无需 printf()、cout 或 puts() 即可打印的 C 等效代码,我没有任何限制

Yes you could call the function directly, however your reasoning to do so is flawed. 是的,您可以直接调用该函数,但是您的推理是有缺陷的。 The time you save by eliminating the subroutine call to the operator is negligible when compared to the time taken to perform the actual function; 与执行实际功能所用的时间相比,通过消除对操作员的子程序调用来节省的时间可以忽略不计; this would be like closing the windows of your car while the convertible roof is down in order to reduce the rain. 这就像关闭汽车的窗户,而敞篷车顶下,以减少雨。

The time required to make a function call is much , much smaller than the amount of time it takes to print your string. 做一个函数调用所需的时间是多少 ,比所需的时间来打印字符串量小得多。 The amount of time you might save with your approach can (and usually should) be ignored. 使用您的方法可以节省的时间量(通常应该)可以忽略。

If you want portability across all standards compliant C++ implementations, you can print a string to standard output in the following ways 如果您希望在所有符合标准的C ++实现中实现可移植性,则可以通过以下方式将字符串打印到标准输出

const char * str = "Hello World\n";
fprintf(stdout, str);
fputs(str, stdout);
for (int i=0; str[i]!=0; ++i)
    putchar(str[i]);
for (int i=0; str[i]!=0; ++i)
    putc(str[i], stdout);
for (int i=0; str[i]!=0; ++i)
    fputc(str[i], stdout);
fwrite(str, sizeof(*str), strlen(str), stdout);

Additionally, you can use std::cerr and std::clog . 另外,您可以使用std::cerrstd::clog They write to stderr instead of stdout , but from the user's perspective, that's often the same place: 他们写入stderr而不是stdout ,但从用户的角度来看,这通常是相同的地方:

std::cerr << str;
std::clog << str;

From an efficiency perspective, I doubt any of these are going to help you. 从效率的角度来看,我怀疑这些都会对你有所帮助。 For that purpose, you might want to look at something a bit more platform specific. 为此,您可能希望看一些更具体的平台。 For POSIX systems, see the answer given by Dave S . 对于POSIX系统,请参阅Dave S给出的答案。 For Windows, see this link . 对于Windows,请参阅此链接

What you shouldn't do, is open up your header files and imitate what they use. 你不应该做的是打开你的头文件并模仿他们使用的东西。 At least, not at the middle levels, where they are using different various obscure functions within their own implementation. 至少,不是在中间层,他们在自己的实现中使用不同的各种模糊功能。 Those functions might not exist upon the next release. 下一版本可能不存在这些功能。 However, if you go to the deepest levels, you will find OS specific calls like the ones in the link I provided above. 但是,如果你进入最深层次,你会发现特定于操作系统的调用,就像我上面提供的链接中那样。 Those should be safe to use as long as you stay on the same OS, or even between OS versions. 只要您停留在相同的操作系统,甚至操作系统版本之间,那些应该是安全的。

On a UNIX type system, you can do the following. 在UNIX类型系统上,您可以执行以下操作。

#include <unistd.h>
#include <stdio.h>

int main()
  {
  const char x[] = "Hello World!";
  write(STDOUT_FILENO, x, strlen(x)); // Feel free to check the return value.
  return 0;
  }

I'm curious what your motivation for doing this would be. 我很好奇你这样做的动机是什么。 Outside of signal handlers, I'm reluctant to go to the lower level calls like this. 在信号处理程序之外,我不愿意像这样进入较低级别的呼叫。 The performance of the I/O will be the primary driver of time, not the intermediate function calls which are usually fairly heavily optimized / inlined. I / O的性能将是时间的主要驱动因素,而不是通常相当大优化/内联的中间函数调用。

You can directly use system calls. 您可以直接使用系统调用。

http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html

This page, for example, explains linux system calls. 例如,该页面解释了linux系统调用。 You can start from the link I copied, and use many methods using assembly, or to say it in the other way, do something without calling the function of it. 你可以从我复制的链接开始,使用汇编使用很多方法,或者用另一种方式说出来,不用调用函数就可以做一些事情。

But I'm guessing that was a trick question and if I had a company, I would never hire a person that uses system calls instead of functions. 但我猜这是一个棘手的问题,如果我有一家公司,我绝不会雇用一个使用系统调用而不是函数的人。

This is an example of using sys_write(4) with standart output(1). 这是使用带有标准输出(1)的sys_write(4)的示例。 You can inline assembly codes into your C/C++ code. 您可以将汇编代码内联到C / C ++代码中。 http://docs.cs.up.ac.za/programming/asm/derick_tut/#helloworld http://docs.cs.up.ac.za/programming/asm/derick_tut/#helloworld

The extraction operator is overloaded in the ostream class. So you cannot actually use it without having an object of that class with it.提取运算符在 ostream class 中过载。因此,如果没有 class 的 object,您将无法实际使用它。

It is implemented in the following manner:它是通过以下方式实现的:

cout<<"Hii"; 

is equivalent to:相当于:

cout.operator<<("Hii")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM