c++,对txt文件进行读取显示

来源:互联网 发布:网络服务商有哪些 编辑:程序博客网 时间:2024/05/16 11:55
#include "stdafx.h"
#include<iostream>
#include <fstream>  
#include <string>  
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream in("w.txt");   
    string filename;  
    string line;  

if(in)
{
//fread(buffer,sizeof(char),100,stream);
//m=strlen(buffer);
while (getline (in, line)) // line中不包括每行的换行符  
        {   
            cout << line << endl;   
        }  
}
//}
//fclose(stream);
return 0;

}

文件中存有信息是hello world,输出也是这个。主要运用了getline函数,如果用fread函数由于不是很很会,会出现乱码,没有geline简便。

0 0