ofstream没有在linux上工作 [英] ofstream not working on linux

查看:905
本文介绍了ofstream没有在linux上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测试代码:

  #include< string& 
#include< iostream>
#include< fstream>

int main(){
std :: ofstream strm = std :: ofstream(test.txt);
strm<< TEST123;
strm.close()
return 0;
}

如果我在windows上编译这个工作完美。但是当我使用以下命令在debian上编译它:
g ++ - 4.7 -std = c ++ 0x -lpthread TestStream.cpp -ldl -o TestStream
比它提供以下输出:
>



我已Google错误无效。有人知道如何解决这个问题吗?我在我的项目中使用了很多的流,并希望编译它在linux也。



编辑:所以我有它编译现在感谢WinterMute但现在它打印空文件。我如何解决这个问题?



EDIT2:dunno为什么,但第二次编译它工作。 thx!

解决方案

使用

  std :: ofstream strm(test.txt); 

这:

 code> std :: ofstream strm = std :: ofstream(test.txt); 

需要一个复制构造函数 std :: ofstream 没有或一个移动构造函数只有从C ++ 11可用。 GCC 4.7没有完全支持C ++ 11,显然这是缺少的功能之一。



在注释中,T.C.提到可移动流不会来到gcc,直到版本5,这是计划在今年发布。这使我惊讶,因为gcc声称完全C ++ 11支持4.8.1版本 - 这是真正的编译器,但不是libstdc ++。现实咬人。



因此,也许值得一提的是 libc ++ < a>(与clang和llvm关联的c ++标准库实现)实现了可移动流,而clang 3.5和gcc 4.9(那些是我在这里并尝试的)编译原始代码,如果它被使用,而不是libstdc ++。 p>

i have a simple test code:

#include <string>
#include <iostream>
#include <fstream>

int main() {
std::ofstream strm = std::ofstream("test.txt");
strm << "TEST123";
strm.close();
return 0;
}

if i compile this on windows it works perfectly. however when i compile it on debian with the following command: g++-4.7 -std=c++0x -lpthread TestStream.cpp -ldl -o TestStream than it gives the following output:

i have googled this error to no avail. does anybody know how to fix this? i use a lot of ofstreams in my projects and would like to compile it on linux also.

EDIT: so i have it compiling now thanks to WinterMute however now it prints empty files. how do i fix this?

EDIT2: dunno why but second time compiling it worked. thx!

解决方案

Use

std::ofstream strm("test.txt");

This:

std::ofstream strm = std::ofstream("test.txt");

requires a copy constructor that std::ofstream doesn't have or a move constructor that is available only since C++11. GCC 4.7 does not have full support for C++11 yet, and apparently this is one of the missing features.

In the comments, T.C. mentions that moveable streams won't come to gcc until version 5, which is planned to be released this year. This took me by surprise because gcc claimed full C++11 support with version 4.8.1 -- which is true for the compiler, but not for libstdc++. Reality bites.

So it is perhaps worth mentioning that libc++ (a c++ standard library implementation affiliated with clang and llvm) implements moveable streams, and both clang 3.5 and gcc 4.9 (those are the ones I have here and tried) compile the original code if it is used instead of libstdc++.

这篇关于ofstream没有在linux上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆