是以下技巧的改进版本:
调试技巧:输出信息到文本文件,并用另一个exe读取以实时显示
使用了批处理文件(.bat)来一键多次启动单个exe,并传入相应参数来指定实时读取显示哪个文件,便捷性大大提升。
以下是示例代码:
1.用来写入数据到多个文本文件的(writer.cpp):
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
const int n=3;
ofstream out[n];
for(int i=0; i<n; ++i)
{
string filename;
filename+='1'+i;
filename+=".txt";
out[i].open(filename.c_str(),ios::out);
}
for(int r=0;;++r)
{
for(int i=0; i<n; ++i)
{
out[i].seekp(0);
out[i]<<i+1<<':'<<r<<endl;
}
}
for(int i=0; i<n; ++i)
out[i].close();
return 0;
}
2.用来根据传入参数实时读取对应文件并显示的(shower.cpp):
#include<iostream>
#include<fstream>
#include<windows.h>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
string 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;
}
3.批处理脚本
start shower.exe 1
start shower.exe 2
start shower.exe 3
批处理脚本创建方式:新建txt文件,在其中输入相应命令后,另存为.bat后缀的文件即可
效果:
基于文本文件实时读取的多黑窗显示方式 进阶版 效果