简体   繁体   English

ifstream,Getline返回一个内存地址

[英]ifstream, Getline returns a memory address

i have the following code: 我有以下代码:

std::ifstream report( fileToRead );
cout << "leyendo archivo: " << fileToRead << endl;
std::string line;
cout << std::getline(report,line) << endl;
report.close();

But cout give the following result: 0x28fe64 What can I do? 但是cout给出以下结果:0x28fe64我该怎么办?

Base on this reference, http://www.cplusplus.com/reference/string/string/getline/ . 基于此参考, http://www.cplusplus.com/reference/string/string/getline/

The getline function return reference to an ifstream object, that is the reason you get an address printed. getline函数返回对ifstream对象的引用,这就是您获取地址的原因。

You need cout << line; 您需要cout << line; to print out the content read. 打印出读取的内容。

I also failed to repeat your bug, which version of g++ and OS you are using? 我也没有重复您的错误,您正在使用哪个版本的g ++和OS? My g++ as following: 我的g ++如下:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.3.0 Thread model: posix 配置为:--prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / usr / include / c ++ / 4.2.1 Apple LLVM版本6.1.0(clang-602.0。 53)(基于LLVM 3.6.0svn)目标:x86_64-apple-darwin14.3.0线程模型:posix

#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char * argv[]){
    char * fileToRead= "a.txt";
    std::ifstream report( fileToRead );
    cout << report << endl;

    cout << "leyendo archivo: " << fileToRead << endl;
    std::string line;
    cout << std::getline(report,line) << endl;
    cout << std::getline(report,line) << endl;
    cout << std::getline(report,line) << endl;
    cout << std::getline(report,line) << endl;
    cout << std::getline(report,line) << endl;
    cout << std::getline(report,line) << endl;
    report.close();
}

The result I got is: 我得到的结果是:

1
leyendo archivo: a.txt
1
1
1
0
0
0

It seems that the ifstream is converted to boolean variable indicating whether there are any file operation error with the ifstream. 似乎ifstream已转换为boolean变量,指示ifstream是否存在任何文件操作错误。 The final three zero is due to there is no more lines to read. 最后三个零是由于没有更多的行可读取。

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

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