qq_15112363 2014-12-14 08:54 采纳率: 20%
浏览 3443
已采纳

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


  • 写回答

4条回答 默认 最新

  • Landpack 2014-12-15 13:15
    关注

    我给你的代码只是映射简单例子:
    而注释给啦一些我的思路。
    其实对于你想要的这种效果,你还应该理解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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿