如何确定天气ostream是文件或控制台流.在下面的程序中我想打印"Hello file!" 写入文件和"Hello console!"时 在写入控制台时.我应该在第17行指定什么条件?
#include <fstream>
#include<iostream>
#include <string>
using namespace std;
class A{
public:
A(string msg):_str(msg){}
string str()const {return _str;};
private:
string _str;
};
ostream & operator << (ostream & os, const A & a)
{
if (os is ofstream) //this is line 17
os << "Hello file! " << a.str() << endl;
else
os << "Hello console! " << a.str() << endl;
return os;
}
int main()
{
A a("message");
ofstream ofile("test.txt");
if (!ofile)
cerr << "Unable …Run Code Online (Sandbox Code Playgroud) 如果我想编写自己的test.cpp来检查另一个.cpp文件是否正在输出我希望它输出的方式,那么无论如何都没有明确打印它吗?
换句话说,有什么比如
assert(output_of_file_being_tested, "this is the correct output");
Run Code Online (Sandbox Code Playgroud)
其中output_of_file_being_tested应该是"cout"ed.
我有一个库,它使用 std::cout 和 std::cerr 将消息发送到控制台。
我知道,我可以在我的 GUI 应用程序中重定向它。但是,我想知道的是,是否有办法检测在消息实际刷新之前是否使用了 cout 或 cerr?
背景:如果文本来自cerr,我希望能够更改文本颜色并在消息前面放置一个类似“错误:”的字符串。
例如,如果有一条线
cerr << "File not found" << endl;
Run Code Online (Sandbox Code Playgroud)
在库中,是否有一种回调或事件可用于确保我可以执行
cerr << "Error: ";
Run Code Online (Sandbox Code Playgroud)
从我这边之前的原始错误信息而不触及原始库?
请注意,在问题中重定向到字符串将stdout/stderr 重定向到字符串需要我知道库何时创建输出并对该字符串执行某些操作。相反,我正在寻找一种自动机制。
之后freopen-ing stdout,我怎样才能打印终端?
freopen("out", "w", stdout); // reopen stdout
/* something */
printf("Now I want to print this on terminal");
Run Code Online (Sandbox Code Playgroud) 我正在阅读freopen()并意识到,如果我们为其指定 stdin/stdout,即使我们使用 cin/cout 进行编码,该函数也将起作用。
研究了一下,我发现这个链接 freopen() 相当于 c++streams,其中一位用户回答:
来自 C++ 标准 27.3.1:
“该对象cin控制来自与该对象关联的流缓冲区的输入stdin,在 中声明<cstdio>。”
所以根据标准,如果我们重定向stdin它也会重定向cin。反之亦然cout。
在 CPPReference 上也看到了类似的内容:
http://en.cppreference.com/w/cpp/io/cin
http://en.cppreference.com/w/cpp/io/cout
全局对象 std::cout 和 std::wcout 控制输出到实现定义类型(派生自 std::streambuf)的流缓冲区,与标准 C 输出流 stdout 关联。
这就是有点令人困惑的地方,因为我也在阅读有关刷新的内容,并注意到 fflush(stdout) 根本无法与 cin/cout 一起使用。
例如,此示例代码不会打印任何内容:
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cout << "Please, enter a number: \n";
fflush(stdout);
cin >> n;
}
Run Code Online (Sandbox Code Playgroud)
下面的代码将打印到output.txt:
#include <cstdio> …Run Code Online (Sandbox Code Playgroud) 我创建了一个基本的控制台应用程序 我想知道如何获取输出并将其写入文本文件.
所以我熟悉C++并在写入文本文件时遇到这个问题它还会写入内存位置吗?这是我的源代码.
std::ofstream userprefs;
srand(time(0));
int studentID = rand() % (99999 - 4 * 55) + 5 / 2;
std::string studentIDString = std::to_string(studentID);
userprefs.open("studentInformation.txt", std::ofstream::out | std::ofstream::app);
std::cout << "The Student ID is sID-"+ studentIDString +" (Copy everything including the sID.\nyou can also find this in the studentInformation.txt file.)"<< std::endl;
userprefs << std::cout << "the sID is sID-"+ studentIDString << std::endl;
userprefs.close();
Run Code Online (Sandbox Code Playgroud)
当我打开它生成的文本文件时,我得到的是我假设的内存?
0x804b164 the sID is sID-33921
Run Code Online (Sandbox Code Playgroud)
如何将其隐藏或仅生成此内容?如果不是记忆,有人可以告诉我这究竟是什么吗?我还想指出我正在使用G ++,并将build标志设置为C++ 11.
我只是想知道你是否有某种方式可以做到这一点:
ofstream exemple (name);
exemple << Display();
Run Code Online (Sandbox Code Playgroud)
Display()是一个void方法,只做类似的事情:
cout << "Something" << endl;
Run Code Online (Sandbox Code Playgroud)
我会这样做,因为我已经为每个类编写了所有方法Display(),我想将它们发送到cout的内容放入我的文件中,或者重新创建一些方法"string Display()".
可能吗?
谢谢!马尔科