//使用(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));
cout << buf << endl;
}
ifs.close();
}
int main(int argc, char* argv[])
{
get();
getline();
return 0;
}
温馨提示:答案为网友推荐,仅供参考