C++ 读取本地文件,并显示。可以一行一行的显示。

来源:互联网 发布:淘宝元旦有双11优惠么 编辑:程序博客网 时间:2024/06/05 20:18

 

//包含文件
#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编译器中编译通过,以后这个可以用在服务端上用来读取文本文件的内容或者是键盘记录的内容。