- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图将一些调试输出添加到 C++03 项目中,但得到了一些奇怪的结果。这是简化的测试代码:
#include <fstream>
int main()
{
{
std::ofstream file("/tmp/test.txt");
file << "hello" << " ... OK, this works\n";
}
std::ofstream("/tmp/test.txt",std::ios_base::app) << "hello"
<< " ... no, I mean hello!\n";
}
出于某种原因,这是我在编译后得到的:
$ g++ test.cpp -o test && ./test && cat /tmp/test.txt
hello ... OK, this works
0x80487fe ... no, I mean hello!
为什么在将字符串输出到未命名的 std::ofstream
对象时得到一个十六进制数?为什么第二个字符串的后续输出有效?
最佳答案
通常operator<<
我们用它来将 C 字符串传递给 std::ostream
是declared as一个免费的功能
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
const char* s );
无名std::ofstream
object 是一个临时变量,临时变量不能绑定(bind)到非常量引用,因此这个运算符重载不参与重载决议。相反,取最接近的匹配,一个成员函数
std::basic_ostream& std::basic_ostream::operator<<(const void*);
,它接受一个类型删除的指针并只打印它的值。由于可以使用临时对象调用成员函数,所以这个确实可行。这解释了输出中的十六进制数。现在,这个运算符返回一个引用,std::basic_ostream&
.由于这不再是一个临时对象,而是对某个非常量对象的引用,通常的自由函数重载 operator<<
,这需要 const char*
, 可以调用成功。这就是第二个字符串按预期打印的原因。
请注意,自 C++11 起,代码将按预期工作,因为我们有一个额外的重载 operator<<
,它采用右值引用:
template< class CharT, class Traits, class T >
basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os,
const T& value );
,并且临时对象确实绑定(bind)到右值引用。
要使代码在 C++03 中工作,您可以使用成员函数 std::ostream::flush()
,它返回对该对象的非常量引用并且对 fstream
没有任何用户可见的副作用对象:
#include <fstream>
int main()
{
std::ofstream("/tmp/test.txt").flush() << "hello"
<< " ... OK, this now works too\n";
}
关于c++ - 为什么将字符串输出到未命名的 std::ofstream 却给我一个十六进制数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45085960/
我想在一个函数中打开一个文件,将打开的文件对象返回给 main,并在另一个函数中使用它函数来填充文件。编译器似乎告诉我,我正在尝试访问 iostream 的私有(private)成员。有没有办法做到这
我最近写了一个记录器类。编译这段代码时: std::ofstream *stream = new std::ofstream; stream->open( log_file_name.c_str()
我有以下测试代码,其中有一个参数 fS,它是 ofstream 的容器: #include #include #include #include int
我正在尝试实现一个记录器,它可以注册到多个流,如 ostringstream、ofstream 等。我试图实现这样的功能 void register_stream(std::ostream& a);
我有一个在线程上写入文件的类 class A{ public: void writeToFile(ofstream& outFile, obj &a) { //...
我有以下代码 void _log_message(const char* fmt, ...) { va_list arg; ofstream logfile; cout << "Open Lo
我刚刚开始阅读如何打开和编辑文件。使用 ifstream 时,如果文件不存在,则不会创建它。 引用下面的代码,条件 (!outfile) 何时为假,就好像文件不存在一样,构造函数将简单地创建它,因此总
#include #define LOGFATAL(msg) log(0, msg) std::ofstream *logst = NULL; void log(int sev, char *msg
我在使用 ofstream 时遇到问题一次将两个不同的输出写入两个不同的文件。程序编译运行正常,并向p1output.txt写入数据,但是当我打开p2output.txt时,除了第一行内容外是空白的:
我有一个“资源模块”类来管理我的应用程序的所有资源,它主要从一个包含所有内容的文件“RESOURCES.DAT”中读取。 从文件中请求新数据的所有对象都通过 ResourceModule,因此我可以管
我遇到流问题,程序无法打开我要写入的文件。最小完整可变代码如下: #include #include using namespace std; int main(){ string roo
我已经研究了好几个小时了,我只知道答案很简单。似乎无论我做什么我都无法打开文件。这是一个多类程序,所以在标题中我有 #include #include class A{ string path
所以我想写一个可以像这样使用的缩进输出类: Debug f; f.open("test.txt"); f }' and '') ofs #include using namespace std;
大家好... 抱歉我的英语不好,但是会说西类牙语... 这周,为了这个proyect的学习和工作,我想创建一个软件来制作文件(.us)... 示例 char name[50]; //Or string
我不想在 main() 中构造 ofstream。这是我所做的,但它没有编译: #include using namespace std; class test { private: ofs
谁能告诉我这是怎么回事? #include #include #include #include #include class writeManager { std::vector
我有一个 C++ 类,它将其数据写出到二进制 std::ofstream。该类将数据存储为 boost:shared_array 但我已经消除了这个问题。问题出在 ofstream 上对 write(
在我的程序中,我的 dataout: void outfile 没有写入文件,任何人都可以找出原因吗? using namespace std; #include #include #include
我想在文件上写点东西。但它只是没有像它必须假设的那样编写。 void main() { int accno; char name[20]; float deposit;
我遇到了一个奇怪的问题。我有两个函数,它们都有一个通过引用传递的 ofstream。但是,当我调用第二个函数时,正在打印第一个函数的一部分。 这是第一个函数: void GamePlay::dealD
我是一名优秀的程序员,十分优秀!