mstar面试题 文件目录中有升级包SOFT001.bin,SOFT002.bin,002比001的版本要新,如何写程序识别出最新的升级包。
2017-06-06 23:46
316 查看
面试题文件目录中有升级包SOFT001.bin,SOFT002.bin,002比001的版本要新,如何写程序识别出最新的升级包。
运行结果
./a.out
pstuDirent d_name[zlib-1.2.8.tar.gz] d_ino[413168] d_off[187304008] d_reclen[32] d_type[8]
pstuDirent d_name[test.out] d_ino[417292] d_off[268019727] d_reclen[20] d_type[8]
pstuDirent d_name[a.out] d_ino[391811] d_off[328475253] d_reclen[20] d_type[8]
pstuDirent d_name[test.c] d_ino[417290] d_off[367818067] d_reclen[20] d_type[8]
pstuDirent d_name[New0001.c] d_ino[413065] d_off[387363692] d_reclen[24] d_type[8]
pstuDirent d_name[curl-7.51.0] d_ino[413488] d_off[424798140] d_reclen[24] d_type[4]
pstuDirent d_name[SOFT112.bin] d_ino[413074] d_off[509853290] d_reclen[24] d_type[8]
pstuDirent d_name[1SOFT111.bin] d_ino[413071] d_off[530715160] d_reclen[24] d_type[8]
pstuDirent d_name[SOFT111.txt.bin] d_ino[413072] d_off[622456054] d_reclen[28] d_type[8]
pstuDirent d_name[SOFT002.bin] d_ino[413070] d_off[708605345] d_reclen[24] d_type[8]
pstuDirent d_name[csdn.c] d_ino[391805] d_off[813129366] d_reclen[20] d_type[8]
pstuDirent d_name[1.txt] d_ino[413063] d_off[921009042] d_reclen[20] d_type[8]
pstuDirent d_name[curl-7.51.0.tar.gz] d_ino[413166] d_off[971975287] d_reclen[32] d_type[8]
pstuDirent d_name[2.txt] d_ino[413064] d_off[982198225] d_reclen[20] d_type[8]
pstuDirent d_name[opendir.c] d_ino[413067] d_off[983528440] d_reclen[24] d_type[8]
pstuDirent d_name[zlib-1.2.8] d_ino[413169] d_off[1002493285] d_reclen[24] d_type[4]
pstuDirent d_name[123.rmvb] d_ino[417304] d_off[1011954566] d_reclen[20] d_type[8]
pstuDirent d_name[ffmpeg_learn] d_ino[417309] d_off[1273990568] d_reclen[24] d_type[4]
pstuDirent d_name[ProFFmpeg] d_ino[417293] d_off[1310706302] d_reclen[24] d_type[4]
pstuDirent d_name[ffmpeg-3.1.6] d_ino[392753] d_off[1345889369] d_reclen[24] d_type[4]
pstuDirent d_name[yasm-1.3.0] d_ino[407880] d_off[1381087985] d_reclen[24] d_type[4]
pstuDirent d_name[.] d_ino[391804] d_off[1451847256] d_reclen[16] d_type[4]
pstuDirent d_name[..] d_ino[2] d_off[1486652463] d_reclen[16] d_type[4]
pstuDirent d_name[ffmpeg-3.1.6.tar.gz] d_ino[392754] d_off[1594763545] d_reclen[32] d_type[8]
pstuDirent d_name[SOFT666660.bin] d_ino[413075] d_off[1671722948] d_reclen[28] d_type[8]
pstuDirent d_name[SOFT233.bin] d_ino[413068] d_off[1725054639] d_reclen[24] d_type[4]
pstuDirent d_name[SOFT1.txt] d_ino[413073] d_off[1749282150] d_reclen[24] d_type[8]
pstuDirent d_name[New0002.c] d_ino[413066] d_off[1768036029] d_reclen[24] d_type[8]
pstuDirent d_name[simple.out] d_ino[417306] d_off[1865870150] d_reclen[24] d_type[8]
pstuDirent d_name[learn] d_ino[413054] d_off[1921161435] d_reclen[20] d_type[4]
pstuDirent d_name[sdasdSOFT1asd.bin] d_ino[413069] d_off[1936555055] d_reclen[32] d_type[4]
pstuDirent d_name[simple_ffmpeg_player.c] d_ino[417303] d_off[2080791565] d_reclen[36] d_type[8]
pstuDirent d_name[yasm-1.3.0.tar.gz] d_ino[407879] d_off[2080931987] d_reclen[32] d_type[8]
pstuDirent d_name[output.yuv] d_ino[417308] d_off[2138376756] d_reclen[24] d_type[8]
pstuDirent d_name[mySDLFirst.out] d_ino[417291] d_off[2147483647] d_reclen[28] d_type[8]
----------------------------
chUpgradefilename[SOFT666660.bin] nMaxVerNum[666660]
root@ubuntu:/share#
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #define SOFT_STR "SOFT" int get_version_num_by_filename(const char *filename,int *pnVerNum) { int nVerNum = 0; char *pchBin = NULL; char *pchS = NULL; if(NULL == filename || 0 == strlen(filename)) { return -1; } if(strncmp(filename,SOFT_STR,strlen(SOFT_STR))!= 0)//保证以SOFT_STR开头 { return -1; } pchBin = strstr(filename,".bin"); if(!(NULL != pchBin && 0 == strcmp(pchBin,".bin"))) //保证以.bin末尾 { return -1; } pchS = filename + strlen(SOFT_STR); while(pchS != pchBin) { if(*pchS > '9' || *pchS < '0') { return -1; } nVerNum = nVerNum*10+(*pchS-'0'); pchS++; } *pnVerNum = nVerNum; return 0; } int isDir(int type) { if(type == 8) //file { return 0; } else if(type == 4) //dir { return 1; } else { return -1; } } int GetMaxVersionAndFilename(const char *pathname,char* pchUpgradefilename,int *pnVerNum) { struct dirent *pstuDirent = NULL; DIR *pDirpathTmp = NULL; int nVerNum = 0; int nMaxVerNum = -1; char chUpgradefilename[255] = {0}; if(NULL == pathname || 0 == strlen(pathname) || NULL == pchUpgradefilename || NULL == pnVerNum) { return -1; } pDirpathTmp = opendir(pathname); if(pDirpathTmp == NULL) { printf("\n if(pDirpathTmp == NULL) \n"); return -1; } while(NULL != (pstuDirent = readdir(pDirpathTmp))) { printf("\n pstuDirent d_name[%s] d_ino[%lu] d_off[%ld] d_reclen[%d] d_type[%d]\n", pstuDirent->d_name, pstuDirent->d_ino, pstuDirent->d_off, pstuDirent->d_reclen, pstuDirent->d_type ); if(isDir(pstuDirent->d_type) == 0 && get_version_num_by_filename(pstuDirent->d_name,&nVerNum) == 0) { if(nVerNum > nMaxVerNum) { memset(chUpgradefilename,0,sizeof(chUpgradefilename)); strncpy(chUpgradefilename,pstuDirent->d_name,sizeof(chUpgradefilename)-1); nMaxVerNum = nVerNum; } } } closedir(pDirpathTmp); printf("\n----------------------------\n"); printf("\nchUpgradefilename[%s] nMaxVerNum[%d]\n",chUpgradefilename,nMaxVerNum); strncpy(pchUpgradefilename,chUpgradefilename,strlen(chUpgradefilename)+1); *pnVerNum = nMaxVerNum; return 0; } int main() { //getcwd int nMaxVerNum = -1; char chUpgradefilename[255] = {0}; GetMaxVersionAndFilename("/share",chUpgradefilename,&nMaxVerNum); return 0; }
运行结果
./a.out
pstuDirent d_name[zlib-1.2.8.tar.gz] d_ino[413168] d_off[187304008] d_reclen[32] d_type[8]
pstuDirent d_name[test.out] d_ino[417292] d_off[268019727] d_reclen[20] d_type[8]
pstuDirent d_name[a.out] d_ino[391811] d_off[328475253] d_reclen[20] d_type[8]
pstuDirent d_name[test.c] d_ino[417290] d_off[367818067] d_reclen[20] d_type[8]
pstuDirent d_name[New0001.c] d_ino[413065] d_off[387363692] d_reclen[24] d_type[8]
pstuDirent d_name[curl-7.51.0] d_ino[413488] d_off[424798140] d_reclen[24] d_type[4]
pstuDirent d_name[SOFT112.bin] d_ino[413074] d_off[509853290] d_reclen[24] d_type[8]
pstuDirent d_name[1SOFT111.bin] d_ino[413071] d_off[530715160] d_reclen[24] d_type[8]
pstuDirent d_name[SOFT111.txt.bin] d_ino[413072] d_off[622456054] d_reclen[28] d_type[8]
pstuDirent d_name[SOFT002.bin] d_ino[413070] d_off[708605345] d_reclen[24] d_type[8]
pstuDirent d_name[csdn.c] d_ino[391805] d_off[813129366] d_reclen[20] d_type[8]
pstuDirent d_name[1.txt] d_ino[413063] d_off[921009042] d_reclen[20] d_type[8]
pstuDirent d_name[curl-7.51.0.tar.gz] d_ino[413166] d_off[971975287] d_reclen[32] d_type[8]
pstuDirent d_name[2.txt] d_ino[413064] d_off[982198225] d_reclen[20] d_type[8]
pstuDirent d_name[opendir.c] d_ino[413067] d_off[983528440] d_reclen[24] d_type[8]
pstuDirent d_name[zlib-1.2.8] d_ino[413169] d_off[1002493285] d_reclen[24] d_type[4]
pstuDirent d_name[123.rmvb] d_ino[417304] d_off[1011954566] d_reclen[20] d_type[8]
pstuDirent d_name[ffmpeg_learn] d_ino[417309] d_off[1273990568] d_reclen[24] d_type[4]
pstuDirent d_name[ProFFmpeg] d_ino[417293] d_off[1310706302] d_reclen[24] d_type[4]
pstuDirent d_name[ffmpeg-3.1.6] d_ino[392753] d_off[1345889369] d_reclen[24] d_type[4]
pstuDirent d_name[yasm-1.3.0] d_ino[407880] d_off[1381087985] d_reclen[24] d_type[4]
pstuDirent d_name[.] d_ino[391804] d_off[1451847256] d_reclen[16] d_type[4]
pstuDirent d_name[..] d_ino[2] d_off[1486652463] d_reclen[16] d_type[4]
pstuDirent d_name[ffmpeg-3.1.6.tar.gz] d_ino[392754] d_off[1594763545] d_reclen[32] d_type[8]
pstuDirent d_name[SOFT666660.bin] d_ino[413075] d_off[1671722948] d_reclen[28] d_type[8]
pstuDirent d_name[SOFT233.bin] d_ino[413068] d_off[1725054639] d_reclen[24] d_type[4]
pstuDirent d_name[SOFT1.txt] d_ino[413073] d_off[1749282150] d_reclen[24] d_type[8]
pstuDirent d_name[New0002.c] d_ino[413066] d_off[1768036029] d_reclen[24] d_type[8]
pstuDirent d_name[simple.out] d_ino[417306] d_off[1865870150] d_reclen[24] d_type[8]
pstuDirent d_name[learn] d_ino[413054] d_off[1921161435] d_reclen[20] d_type[4]
pstuDirent d_name[sdasdSOFT1asd.bin] d_ino[413069] d_off[1936555055] d_reclen[32] d_type[4]
pstuDirent d_name[simple_ffmpeg_player.c] d_ino[417303] d_off[2080791565] d_reclen[36] d_type[8]
pstuDirent d_name[yasm-1.3.0.tar.gz] d_ino[407879] d_off[2080931987] d_reclen[32] d_type[8]
pstuDirent d_name[output.yuv] d_ino[417308] d_off[2138376756] d_reclen[24] d_type[8]
pstuDirent d_name[mySDLFirst.out] d_ino[417291] d_off[2147483647] d_reclen[28] d_type[8]
----------------------------
chUpgradefilename[SOFT666660.bin] nMaxVerNum[666660]
root@ubuntu:/share#
相关文章推荐
- c/s程序版本自动升级的问题,如何判断client端版本号是否最新,然后从指定ftp服务器down
- c/s程序版本自动升级的问题,如何判断client端版本号是否最新,然后从指定ftp服务器down
- discuz!x二次开发识别程序目录和文件列表
- JAVA 程序中如何拷贝一个目录下的文件及子目录到另一个目录,如何获取系统环境变量等...
- winform程序在编译时,如何自动将文件生成到debug目录下?
- 最新Wordpress 3.5版本如何修改图片默认上传目录路径
- ubuntu系统如何将firefox升级到最新版本
- mfc对话框程序在ide编译可运行,但编译完毕的debug和release版本不能运行。。后来发现原因是:用到的sqlite数据库文件没在对应目录下,而代码中没有try..catch..
- 猛犸代码助手开通官网,最新版本实现程序自动更新升级
- 【图文教程】Win10 Build 9841如何升级到最新版本Build 9860?
- 彻底删除文件(File Delete Absolutely) 最新3.01版本 也已经在中关村在线升级成功
- 如何升级openssl到最新版本 ubuntu
- Discuz!X论坛开发(二)二次开发 教你识别程序目录和文件列表for discuzx!1--NO.1
- 如何将Metro 程序需要的文件加入到app运行目录(AppX文件夹中)
- Wordpress最新3.5版本如何修改图片默认上传目录路径
- 如何安装 Microsoft Office 兼容包,以便能够使用早期版本的 Excel、PowerPoint 和 Word 来打开和保存来自 2007 Office 程序的文件
- Discuz!X论坛开发(六)二次开发 教你识别程序目录和文件列表for discuzx!1--NO.5
- Wordpress最新3.5版本如何修改图片默认上传目录路径
- Discuz!X论坛开发(七)二次开发 教你识别程序目录和文件列表for discuzx!1--NO.6
- 如何在线升级Linux mint 14至最新版本的Linux mint 15