C++写一个打开文件的百宝箱

来源:互联网 发布:cmd 查看80端口占用 编辑:程序博客网 时间:2024/05/16 05:50

作为一名程序员,电脑桌面也只有自己能看得清,上面一大堆的快捷方式、文件、等等一大堆的东西,最近正在研究C++,就想着写一个在Windows下能像Linux下alias命令一样,给应用程序起一个好记的别名

本文涉及到的知识:

读文件、判断文件是目录还是文件、打开其他文件、带参数的主函数

我的想法的这样的:

先写好一个配置文件(C:\path.ini)

程序运行时,读取 "C:\path.ini" 文件里的设置,

C:\path.ini文件格式:
name=path;
eg:

应用程序
eclipse=D:\android\eclipse\eclipse.exe;
ps=D:\\Program Files (x86)\\Adobe Photoshop CS6\\Photoshop.exe;

or

Windows自带的应用程序
explorer=explorer;

or

目录
apk=Z:\MST628_Base\MST628_jb4.4-kikat\device\mstar\common\apps;

注意:别忘记以分号结尾 ;
该程序会根据名称,找到相应的绝对路径,再打开,可再加一个参数
eg:

打开运行,输入cmd,进入到字符界面,再输入以下命令:
open ps C:\abc.png


使用前,请在C盘根路径下,创建path.ini文件

好了,其他的不多说,写代码:

涉及到的所有头文件:

#include  <io.h>
#include <iostream>
#include <windows.h>
#include <shellapi.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>


using namespace std;设置命名空间


涉及到的常量:

const char PATH[] = "C:\\path.ini";
const int MAX_LINE = 1024;
const int FOLDER = 1;
const int FILER = 2;
const int NOTEXIST = -1;


//别名与路径的结构体,别名请不要以中文开头,我也不知道为什么
struct file {
char name[255];
char path[255];
};

这个结构体用来保存应用程序的别名和绝对路径


读文件:

C:\path.ini文件格式是名称=路径;

所以应该一行一行的读


/***
 * 这个方法是读配置文件(C:\\path.ini)
 * 带一个参数:file结构体数组,
 * 将path.ini中的每一行,转换成一个file结构体
 * 返回一个整数,即总共有多少个结构体,也就是行数
 */
int readFile(file files[]) {
//判断C:\path.ini是否可读
if ((_access(PATH, 0)) == -1) {
cout << PATH << " can not find" << endl;
return 0;
}


const char s = '=';//名称与路径的分隔符
const char end = ';';//路径结束符
FILE *fp = fopen(PATH, "r");//以只读方式打开文件
char buf[MAX_LINE];//一行最多1024个字符,每行读出来的字符,就保存在这个数组中
int num = 0;//行号
while (fgets(buf, MAX_LINE, fp) != NULL) {
//按行读文件,直到读到的是NULL,这就是末尾
int i;//这个i 是用来记录'='的位置的
for (i = 0; i < MAX_LINE; i++) {
if (buf[i] == s) {//当遇到'='时,就跳出循环
files[num].name[i] = '\0';
break;
}
files[num].name[i] = buf[i];//把读到的字符保存到结构体files[num]的name数组中
}
// memcpy(files[num].name, buf, i);
//'='前面部分是别名,跳过'='(j=i+1),再把剩下的字符保存到结构体files[num]path数组中
for (int j = i + 1; j < MAX_LINE; j++) {
if (buf[j] == end) {
break;
}
files[num].path[j - i - 1] = buf[j];
}
num++;//每循环一次,行数加1
}
fclose(fp);//文件读完了,记得关闭
return num;//返回行数
}



判断是文件还是目录:

如果是目录,就运行explorer,打开相应的目录,所以先要判断一下

/***
 * 判断是文件还是目录
 * 参数:char*
 * 返回值:1:目录
 * 2:文件
 * -1:出错
 */


int foo(const char* filename) {
struct _stat buf;
int r = _stat(filename, &buf);
if (r == 0) {
if (buf.st_mode & _S_IFDIR) {
// cout << "foo::this is a folder" << endl;
return FOLDER;
} else {
// cout << "foo::this is a file" << endl;
return FILER;
}
} else {
// if ( errno == ENOENT)
// return NOTEXIST;
// else
return -1;
}
}


/***
 * 打开文件
 * 参数:path文件路径
 */


void exec(char* path) {
//只用到三个参数 "open":以打开的方式
//path:可执行文件的路径
//SW_SHOW:前端显示
ShellExecuteA(NULL, "open", path, NULL, NULL, SW_SHOW);
}


/***
 * 打开文件
 * 参数:path文件路径,param可执行文件可带的参数
 */
void exec(const char* path, char* param) {
//只用到四个参数 "open":以打开的方式
//path:可执行文件的路径
//param:可执行文件所带的参数,例如:photoshop abc.png
//SW_SHOW:前端显示
ShellExecuteA(NULL, "open", path, param, NULL, SW_SHOW);
}


/***
 * 主函数
 * 可接受参数
 * open www.baidu.com
 * open ps abc.png
 * open calc
 */


int main(int argc, char *argv[]) {
int count;//配置文件中定义别名的个数
file files[MAX_LINE];//最多1024个file结构体
count = readFile(files);//读取配置文件
if (argc == 1) {//如果不带参数时,argc==1,这1代表的是本身的名字
//打开所有设置的项
for (int i = 0; i < count; i++) {
cout << files[i].name << endl;
}
} else if (2 == argc) {//两个参数时
for (int i = 0; i < count; i++) {
//对传进来的参数进行配对,这是对别名file.name匹配
if (strcmp(argv[1], files[i].name) == 0) {//如果有别名配对到了
cout << "open " << files[i].name << endl;
cout << "open " << files[i].path << endl;
if (foo(files[i].path) == FOLDER) {
//判断别名对应的路径是文件还是目录
//如果是目录,就用explorer打开
// cout << "this is a folder" << endl;
exec("explorer", files[i].path);
return 0;
}
//如果不是目录,就按正常程序打开
exec(files[i].path);
return 0;
}
}
//如果别名没匹配到,就按系统默认的方式打开,例如:open www.baidu.com
exec(argv[1]);
} else if (3 == argc) {//传进来了两个参数,例如:open ps C:\abc.png
for (int i = 0; i < count; i++) {
if (strcmp(argv[1], files[i].name) == 0) {//别名匹配
cout << "open " << files[i].name << endl;
cout << "open " << files[i].path << endl;
exec(files[i].path, argv[2]);//把第二个参数传给应用程序
return 0;
}
}
}
return 0;

}


源码下载地址:http://download.csdn.net/detail/ytmfdw/8207623

用PowerCmd,搭配本程序,那就整得跟Linux一样

0 0
原创粉丝点击