C++寫一個打開文件的百寶箱

作爲一名程序員,電腦桌面也只有自己能看得清,上面一大堆的快捷方式、文件、等等一大堆的東西,最近正在研究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一樣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章