是这样的 我用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);
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。