刚从C#开始转C++,有很多东西不理解,如下边这段代码:
#include <fstream>
#include <iostream>
int main() {
using namespace std;
ifstream file;
basic_ifstream <wchar_t> wfile;
char c;
// Open and close with a basic_filebuf
file.rdbuf()->open( "basic_filebuf_close.txt", ios::in );
file >> c;
cout << c << endl;
file.rdbuf( )->close( );
// Open/close directly
file.open( "iotest.txt" );
file >> c;
cout << c << endl;
file.close( );
// open a file with a wide character name
wfile.open( L"iotest.txt" );
// Open and close a nonexistent with a basic_filebuf
file.rdbuf()->open( "ziotest.txt", ios::in );
cout << file.fail() << endl;
file.rdbuf( )->close( );
// Open/close directly
file.open( "ziotest.txt" );
cout << file.fail() << endl;
file.close( );
}
下边是我跟大牛的聊天记录,疑问解开。(有个大牛帮解答疑问感觉就是爽啊!)
C++中为啥ifstream不用new,用完还不用delete,布布扣,bubuko.com
C++中为啥ifstream不用new,用完还不用delete
原文:http://blog.csdn.net/lihb018/article/details/38487509