如何定义ifstream类? 10
如何定义ifstream类?欲利用其对象in、out调用:out.put(c);out.open(OutputFile,ios::binary);in.get(c);...
如何定义ifstream类?欲利用其对象in、out调用:out.put(c);out.open(OutputFile,ios::binary);in.get(c);
展开
1个回答
展开全部
//使用(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;
}
//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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询