c++将读取的文件内容一次性输出于屏幕

是这样的 我用c++实现将很多字符构成的txt文件依次输出 ,从而形成动画的效果
但是在每读取一个txt文件的时候读取的很慢 在控制台输出的时候文件的字符从上往下刷下来,让后清屏 再读第二个文件 这样太慢了 能不能让一个文件的字符一次性显示屏幕上啊?
我的代码:

#include
#include
#include
#include
using namespace std;
void hoop(string x)
{
const char *txtname=x.c_str();
ifstream infile(txtname,ios::in);
if(!infile)
{
cerr<<"open1error!"<<endl;
exit(1);
}
char ch;
while(infile.get(ch))
cout.put(ch);
cout<<endl;
infile.close();
}
int main()
{
string c="F:/字符画/";
string d=".txt";
int ss;
char temp[64];
string str;
for(int i=1;i<3456;i++)
{
ss = i;
sprintf(temp, "%d", ss);
string s(temp);
string a=c+s.c_str()+d;

hoop(a);
system("cls");

}
return 0;
}


我给你的代码只是映射简单例子:
而注释给啦一些我的思路。
其实对于你想要的这种效果,你还应该理解i/o编程
可以参考:blog.csdn.net/querdaizhi/article/details/7436722
若是你觉得还有问题我们继续交流!

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include "tlpi_hdr.h"
#define errExit(msg) (perror(msg),(exit(1)))
int main(int argc,char * argv[])
{
    char * addr;
    int fd;
    struct stat sb;
    if(argc !=2 ||strcmp(argv[1],"--help")==0)
        //usageErr("%s file\n",argv[0]);
        errExit("usage..");

    fd=open(argv[1],O_RDONLY);//Looks ,here your should change as 
    /*
     *while file[i].txt no finish
     *  fd=open(file[i].txt)
     *  mmap(......)
     *  done...
     */
    if(fd==-1)
        errExit("open");
    /*Obtain the size of the file and use it to specify the size of
      the mapping and the size of the buffer to be written */
     if(fstat(fd,&sb)==-1)
         errExit("fstat");
     addr=mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,fd,0);
     if(addr==MAP_FAILED)
         errExit("mmap");
     if(write(STDOUT_FILENO,addr,sb.st_size)!=sb.st_size)
         errExit("partial/failed write");
    exit(EXIT_SUCCESS);
}

在linux下,要是标准输出没有遇到换行的化,是不会立即输出的。因为是代换冲的。一般可以fflush(stream).而对于你想要的效果,我觉得可以考虑
用内存映射,避免read/write读写过慢的缺点。

简单来看可以在你的hoop中定义一个string,用来缓存一个文件中的全部内容。hoop不要一次输出一行,每次读取之后放到string中:

 while(infile.get(ch))
    cout.put(ch);

改成:

    std::string temp;
     while(infile.get(ch))
            temp+=ch;
    cout.put(temp);

这样就行喽:D

问题的关键是
while(infile.get(ch))
cout.put(ch);
cout<<endl;
你是读取一行字符输出一行的。

你需要用一个变量保存完整的一个文件的内容,再整体输出才行。