开发者社区> 问答> 正文

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;
}

展开
收起
a123456678 2016-03-20 09:39:23 2021 0
1 条回答
写回答
取消 提交回答
  • 我给你的代码只是映射简单例子:
    而注释给啦一些我的思路。
    其实对于你想要的这种效果,你还应该理解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);
    }
    2019-07-17 19:08:38
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载