您的位置:首页 > 编程语言 > C语言/C++

notepad++编辑器打开一个文件时,给文件添加信息

2016-06-29 17:08 561 查看
最近在Windows平台上用的notepad++编码,每次打开一个文件时,都是空白的,我希望在我新建一个文件时,自动添加一些C/C++头文件等的信息。也找了一些插件,但是不能用,所以就自己动手做了一个。首先我是把notepad++的路径添加到path里,方便程序调用。下面是源码:

#include <iostream>
#include <cstring>
#include <fstream>
#include <ctime>
#include <io.h>
#include <dirent.h>
#include <cctype>
using namespace std;

//写入头文件和时间,等信息
void writeStuff(ofstream &file)
{
//date and time
time_t now = time(0);
tm *ltm = localtime(&now);
file<< "/*"  << endl << "*日期:" << ltm->tm_year+1900
<< "/" << ltm->tm_mon+1 << "/" << ltm->tm_mday
<< "/" << "\t" << ltm->tm_hour << ":" << ltm->tm_min
<< ":" << ltm->tm_sec << endl << "*";
file<< "需求:" << endl << "*总结:"<< endl << "*作者:" << "ZH" << endl
<< "*联系:1537715899@qq.com" << endl << "*/" << endl << endl
<< "int main()\n{\n\n\treturn 0;\n}" << endl;
}
inline void writeCInclude(ofstream &file)
{
file << "#include <stdio.h>\n" << endl;
}
inline void writeCppInclude(ofstream &file)
{
file << "#include <iostream>\n\nusing namespace std\n" << endl;
}

//判断字符串是否是以flag结尾的
int endOf(const char *string, const char *flag)
{
int end = strlen(string);
if (strcmp(string + end-1, flag) == 0)
return 1;
else
return 0;
}

//判断文件是否已经存在
int ifFileExist(const char *file)
{
long hFile = 0;
struct _finddata_t fileInfo;
string pathName, exdName;
// \\* 代表要遍历所有的类型
if ((hFile = _findfirst(pathName.assign(".").append("\\*").c_str(), &fileInfo)) == -1)
{
return -1;
}
do
{
//判断文件的属性是文件夹还是文件
DIR *pDir;
pDir=opendir(fileInfo.name);
if (!pDir && strcmp(fileInfo.name, file)==0)
{
return 1;
}
} while (_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
return 0;
}

int main(int argc, char *argv[])
{
bool flag = true;  //用于终止判断文件是否存在的循环
ofstream file;
char command[1024];//用于在控制台输出调用文本编辑器的命令
if (strcmp(argv[0], "mkfile") != 0)  return -1;
//判断文件是否存在
while (flag)
{
char filename[10];
char echo[3];
if (ifFileExist(argv[1]))
{
cout << "The file is exist, do you want to cover it?[YES][NO]";
cin >> echo;
if (strcmp(echo, "YES")==0 || strcmp(echo, "yes")==0 || strcmp(echo, "Y")==0 || strcmp(echo, "y")==0)
{
flag = false;
}
else if (strcmp(echo, "NO")==0 || strcmp(echo, "no")==0 || strcmp(echo, "N")==0 || strcmp(echo, "n")==0)
{
cout << "please enter filename again. ";
cin >> filename;
strcpy(argv[1], filename);
continue;
}
}
flag = false;
}
//判断文件类型,并添加适当的信息
if (endOf(argv[1], "p"))
{
file.open(argv[1], ios::trunc);
writeCppInclude(file);
writeStuff(file);
}
else if(endOf(argv[1], "c"))
{
file.open(argv[1], ios::trunc);
writeCInclude(file);
writeStuff(file);
}
else
{
cout << "usage: mkfile filename" << endl;
}
//启动文本编辑器
sprintf(command, "notepad++ %s", argv[1]);
system(command);
file.close();//!!!!记得关闭流
return 0;
}


有需要的可以点击这里下载,也可以进行留言评论,单独发给你。

需要注意的是,这个是控制台程序,你需要打开cmd来运行,用法是mkfile加上你要创建的文件名(中间有空格),这里我只写了C/C++部分,其他的,有需求的,可以自己下载源码自己实现,各部分代码都有注释,相信都能看懂。

这个小程序目前还只能创建一个文件并判断这个文件是否已经存在,存在的话,需要重新输入文件名。

这篇博客会随着学习的深入,不断更新。

我也是个刚学的菜鸟,大家觉得有更好的实现方式,或者更好的方法,大家可以一起讨论。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C-C++ 编辑器 notepad++