-
方法一 popen(可获取命令行执行后的输出结果)
转载自:
1 /* 2 Execute command line commands and get specific results by comparison cTemplate 3 result storage in cResult 4 5 Example: 6 7 d:\SVN>svn info D:\SVN\ZVideoProcessor 8 Path: ZVideoProcessor 9 Working Copy Root Path: D:\SVN 10 URL: http://192.168.2.196/svn/Repos/ZVideoProcessor 11 Relative URL: ^/ZVideoProcessor 12 Repository Root: http://192.168.2.196/svn/Repos 13 Repository UUID: 43546b21-47e8-432e-acd4-bb9acb1f9fba 14 Revision: 1270 15 Node Kind: directory 16 Schedule: normal 17 Last Changed Author: sam.zhen 18 Last Changed Rev: 1268 19 Last Changed Date: 2018-11-01 20:29:28 +0800 (周四, 01 十一月 2018) 20 21 cmd: svn info D:\SVN\ZVideoProcessor 22 cTemplate: Revision: 23 cResult: 1270 24 */ 25 int execmd(char* cmd,char* cResult,char* cTemplate) 26 { 27 int iTemplateSize=strlen(cTemplate); 28 char buffer[128]; 29 FILE* pipe = _popen(cmd, "r");//open pipe ,execute cmd 30 if (!pipe) 31 { 32 cout<<"execute "<<cmd<<"error"<<endl; 33 return -1; 34 } 35 36 while(!feof(pipe)) 37 { 38 if(fgets(buffer, 128, pipe)) 39 { 40 //if cTemplate in buffer,then storage result in cResult 41 int res=strncmp(cTemplate,buffer,iTemplateSize); 42 if(res==0) 43 { 44 strncat(cResult,buffer+iTemplateSize,10); 45 int i=strlen(cResult);//because have '\n' 46 cResult[i-1]='\0'; 47 break; 48 } 49 else 50 { 51 continue; 52 } 53 } 54 } 55 56 _pclose(pipe);//close pipe 57 return 0; 58 } 59 60 int main() 61 { 62 //get svn version 63 char cSvnVersionNum[10]; 64 memset(cSvnVersionNum,0,sizeof(cSvnVersionNum)); 65 execmd("svn info ../../../SVN",cSvnVersionNum,"Revision: "); 66 return 0; 67 }
-
方法二 通过预处理方式
转载自:
1.编写版本模板文件svn_revision_template.h
1 #ifndef _SVN_REISION_H_ 2 #define _SVN_REVISION_H_ 3 #define AMG_LIB_VER_SVN_VERSION "$WCREV$" 4 #endif // !_SVN_REISION_H_
注意$WCREV$这里不能修改
2.新建版本文件svn_revision.h
1 #ifndef _SVN_REISION_H_ 2 #define _SVN_REVISION_H_ 3 #define AMG_LIB_VER_SVN_VERSION "1267" 4 #endif // !_SVN_REISION_H_
3.通过预先生成事件,添加下面的批处理命令
subwcrev.exe .\ .\svn_revision_template.h .\svn_revision.h
注意第一个参数.\ 指需要获取哪个路径的svn号码
第二个参数.\svn_revision_template.h 指模板文件路径
第三个参数.\svn_revision.h 指修改后文件保存路径
4.代码中使用:直接使用.\svn_revision.h文件中的宏
AMG_LIB_VER_SVN_VERSION