基于文本文件实时读取输出的多黑窗显示方式 便捷调用版

目录

优点

示例代码

使用方式

fshow.h

 fshow.cpp

shower.cpp

效果

使用例下载


提醒

似乎还有许多漏洞,待进一步优化完善

漏洞修复1:使用flush()强制输出缓存区内容,解决了文本文件不更新的问题

优点

1.使用title命令为每个窗口标题设置对应名字
2.用类封装,将各操作自动化了,且可非常简便地调用,如输出只需像fstream那样<<就行
3.使用专门的文件夹存储这些文本文件和shower.exe,防止污染项目,且便于管理
4.设置了预防不受控新建窗口的暂停点

示例代码
#include <iostream>
#include "fshow.h"

int main() {
	
	fshow a("a",1,1);
	fshow b("b",1,1);
    fshow c;// 采用默认参数构造
	
	for(int i=1;i<10000000;++i)
	{
		a<<i<<'\n';
		b<<i+999<<'\n';
        c<<-i<<'\n';

		if(i%10==0){
			a.clean();
			b.clean();
            c.clean();
		}
	}
	system("pause");
	return 0;
}
使用方式

1.把下面的fshow.h和源文件放入项目文件夹并加入项目,在需要使用的文件#include"fshow.h"

2.在项目生成exe的目录下新建名为fshowOutput的文件夹,把shower.cpp编译出的shower.exe放入该文件夹

3.在项目中即可方便快捷地使用此功能(可参照使用例,并且头文件也有部分说明)

fshow.h
#ifndef FSHOW_H
#define FSHOW_H

#include<iostream>
#include<fstream>
#include<sstream>
#include<windows.h>

using namespace std;

class fshow
{

		static int cnt;

		string name;
		ofstream ot;
		bool autoClose;
		bool autoClean;

	public:
		// name为对应文件和窗口的名字,
		// autoCls指定该显示窗口是否自动关闭
		// autoCln指定对应文本文件是否自动删除 (前提条件为对应窗口自动关闭)
		fshow(string name="",bool autoCls=0,bool autoCln=1);
		
		~fshow();

		template<class T>
		fshow& operator<<(T toShw)
		{
			ot<<toShw;
			//cout<<toShw<<'\n';
			return *this; 
		}
		
		void clean();
};


#endif
 fshow.cpp
#include "fshow.h"

int fshow::cnt=0;


fshow::fshow(string nam, bool autoCls, bool autoDl)
:autoClose(autoCls),autoDel(autoDl)
{
	//cout<<"constructing\n";
	
	++cnt;
	if(cnt%10==0){
		cout<<"已新建多个窗口,为预防不受控新建,自动暂停\n";
		system("pause"); 
	} 
	
	
	if(nam==""){
		stringstream ss;
		ss<<cnt;
		ss>>nam;
	}
	name=nam;
	
	string filename="fshowOutput\\"+name+".txt";
	
	//cout<<filename<<'\n';
	
	ot.open(filename.c_str(),ios::out);
	
	string sta="start fshowOutput\\shower.exe "+name;
	system(sta.c_str());

}

fshow::~fshow() 
{
	ot.close();
	
	//cout<<name<<endl<<FindWindow(NULL,name.c_str())<<'\n';
	
	if(autoClose)
		// 可能的漏洞:
		// 若构造函数与析构函数间隔时间太短,则会因为窗口没来得及打开而关闭失败 
		SendMessage(FindWindow(NULL,(wchar_t*)name.c_str()),WM_CLOSE,0,0);
	
	if(/*autoClose&&*/autoDel){
		string del="del /f fshowOutput\\"+name+".txt";
		system(del.c_str());
	}
}

void fshow::clean()
{
	ot.seekp(0);
	
	return;
}
shower.cpp

并未使用上一篇博客中更简便的读取显示方法,因为那种方法闪屏严重(可能是读取效率太高了?)

#include<iostream>
#include<fstream>
#include<windows.h>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
	string setTi="title ";
	setTi+=argv[1];
	system(setTi.c_str());
	
	string s="fshowOutput\\";
	s+=argv[1];// argv[0]是程序名,argv[1]才是另外传入的第一个参数
	s+=".txt";
//	cout<<s<<endl;
//	system("pause");
	
	
	ifstream in;
	in.open(s.c_str(),ios::in);
	while(1){
		in.seekg(0);
		cout<<in.rdbuf();
		Sleep(50);
		system("cls");
	}
		
	in.close();
	return 0;
}
效果

基于文本文件实时读取的多黑窗显示方式 1.0.0效果

使用例下载

https://download.csdn.net/download/Library20/88712335?spm=1001.2014.3001.5503

ht·tps://cloud.189.cn/t/jM3QNjENveQr (访问码:8pux)

  • 12
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值