📄 demo_4_file_txt_4.cpp
字号:
//***************************************************
// 文本文件中数据随机读入内存并显示.
//***************************************************
// eof : end of file 文件结束函数 :
// 若遇到文件结束符,函数值为真1,否则为假0.
//***************************************************
# include <fstream.h>
# include <stdlib.h>
int main()
{
fstream file;
file.open("Test.txt",ios::in|ios::out);
if(!file)
{
cerr<<"open error!"<<endl;
exit(1);
}
file<<"1234567890"<<endl;
file<<"abcdefghij"<<endl;
file<<"**********"<<endl;
// cout<<file.tellg()<<endl;
// file.seekg(0,ios::beg); //非常关键
file.seekg(0);
// cout<<file.tellg()<<endl;
char file_line[60];
int i=0;
while(!file.eof()) //若未遇到文件结束符
{
i++;
file.getline(file_line,sizeof(file_line));
cout<<i<<": "<<file_line<<endl;
}
file.close();
return 0;
}
/*
1: 1234567890
2: abcdefghij
3: **********
4:
Press any key to continue
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -