如何定义ifstream类?

来源:百度知道 编辑:UC知道 时间:2024/06/14 00:12:26
如何定义ifstream类?欲利用其对象in、out调用:out.put(c);out.open(OutputFile,ios::binary);in.get(c);

//使用(C++库)ifstream读文件数据
//simple example

#include <iostream>
#include <fstream>
using namespace std;
//文件地址随便改哈。改的格式要相同
#ifdef WIN32
#define TEST_FILE "c:\\tmp\\test.txt"
#else
#define TEST_FILE "/tmp/test.txt"
#endif

void get()
{
//ifstream ifs;
//ifs.open(TEST_FILE);
ifstream ifs(TEST_FILE);

//while (ifs.good()) cout << (char) ifs.get();//simple
while (ifs.good()) {

char ch = 0;
//ch = ifs.get();
ifs.get(ch);

cout << ch;
}

ifs.close();
}

void getline()
{
//ifstream ifs;
//ifs.open(TEST_FILE);
ifstream ifs(TEST_FILE);

while (ifs.good())
{
char buf[1024] = {0};
ifs.getline(buf, sizeof(buf));