//包含文件
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
//主函数
void main()
{
ifstream infile("1.txt");//打开1.txt
string s;
if (!infile)
cout<<"文件打开失败!"<<endl; //不能打开文件,报错
else
{
while(getline(infile,s))
{
cout<<s<<endl;
getch(); //等你按下任意键,再执行下一句
}
}
system("pause"); //调system函数,发出dos命令"pause"
}
//先新建Console程序,再新建c++ source文件,然后把上面的拷贝进去,直接编译即可。
//以上在VC++ 6.0编译器中编译通过,以后这个可以用在服务端上用来读取文本文件的内容或者是键盘记录的内容。
本文介绍了一个使用C++编程语言读取文本文件的基本示例,通过使用标准库中的iostream和fstream来实现文件的打开与逐行读取功能,并在VC++6.0编译器中验证了其正确性。
7048

被折叠的 条评论
为什么被折叠?



