C++文件的建立
#include
#include
int main()
{
using namespace std;
ofstream ofs(“jar.txt”, ios::out);
ofs << “小明” << endl;
ofs << “你好!” << endl;
if (!ofs.is_open())
{
cout << “文件打开失败:” << endl;
return 0;
}
//关闭流
ofs.close();
ifstream fin;
fin.open("jar.txt",ios::in);
if (!fin.is_open())
{
cout << "文件打开失败:" << endl;
}
char s[1024] = { 0 };
读取方式一:按行读取直到文件尾部
//while(fin >> s)
//{
// cout << s << endl;
//}
读取方式二:
/ar s[1024] = { 0 };
//while (!fin.eof())
//{
// fin.getline(s,sizeof(s));
// cout << s << endl;
//}
//读取方式三:单个字符读取
char c;
while ((c = fin.get()) != EOF)
{
cout << c << endl;
}
//关闭流
fin.close();
system("pause");
return 0;
}