C++ 如何将文本文件中的内容输出到屏幕上

RTRTRTRTRTRTRTRT 越简单越好

1、首先要进行文件输出操作,需要包含头文件,#include <fstream>。

2、在进行文件输入输出操作时会用到cin/cout,所以最好指明名称空间 ,using namespace std。

3、建立ofstream对象,如ofstream outfile。

4、把对象和文件进行关联,利用outfile对象把希望输出到文件中的数据输出到文件myfile.txt中。

5、输出完成后,要关闭对象与文件之间的连接,就完成了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-03-03

参考代码如下:

#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main(){
  fstream myfile("myname.txt",ios::in|ios::out);
  string line;
  if(myfile.fail()){
    cerr<<"error oprening file myname!"<<endl;
    exit(-1);
  }
  while(getline(myfile,line))
    cout<<line<<endl;
}

第2个回答  推荐于2018-03-05
ifstream in(a.txt); //a.txt是指定的文本文件
for(string str; getline(in,str);)//getline()逐行读取文本
cout<<str<<endl;本回答被提问者采纳
第3个回答  2010-12-13
如果是VC就可以直接有鼠标操作,复制粘贴
如果是TC的话得用键盘自己输入了本回答被网友采纳
第4个回答  2010-12-13
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string s;
ifstream file;
file.open("C:\\Documents and Settings\\Administrator\\text.txt");
char tem(0);
while(!file.eof ())
{
file.get(tem);
s+=tem;
}
cout<<s;
file.close();
}
相似回答