首页 > 技术知识 > 正文

一. 前言

环境: windows、纯C\C++环境编程。

在文件、目录处理时,经常需要对文件名称、目录名称、文件后缀等数据做处理。在linux下比较方便。有basename可以直接调用,获取文件名称。windows下C、C++标准库里没有现成的函数可以直接提取文件名称、目录名称、剔除文件路径,下面就自己实现了几个方式完成文件名提取。

二. 实现代码-纯C/C++

示例代码:

复制string path = “C:\Users\Administ.1.2.3rator\Desktop\text\data.22.txt”; //string path = “http://cqpc:8001/Uploads/1/220512030806054762.mp4”; //1.获取不带路径的文件名 string::size_type iPos; if (strstr(path.c_str(), “”)) { iPos = path.find_last_of(\) + 1; } else { iPos = path.find_last_of(/) + 1; } string filename = path.substr(iPos, path.length() – iPos); cout <<“获取不带路径的文件名:”<

复制返回结果:

复制复制获取不带路径的文件名:data.22.txt 获取不带后缀的文件名:data.22 获取后缀名:txt 基本名称:data 复制代码

复制三. C语言字符串处理案例

复制1. 计算空格、大小写字母

复制从键盘上输入一个字符串, 计算字符串里有多少个空格、小写字母、大写字母、数字。

复制复制#include //标准输入输出 #include //字符串处理头文件 int main(int argc,char **argv) { int len=0; int i; char str[100]; int cnt[5]={0}; //初始化赋值 //scanf(“%s”,str); //从键盘上录入字符串,字符串结尾: \0 //gets(str); //从键盘上录入字符串 fgets(str,100,stdin); //从键盘上录入字符串 (标准输入) //空格、小写字母、大写字母、数字 其他数据 /*1. 计算字符串的长度*/ while(str[len]!=\0)len++; printf(“len1=%d\n”,len); printf(“len2=%d\n”,strlen(str)); //计算字符串长度 /*2. 处理字符串*/ for(i=0;i=a&&str[i]<=z)cnt[1]++;   else if(str[i]>=A&&str[i]<=Z)cnt[2]++;   else if(str[i]>=0&&str[i]<=9)cnt[3]++;   else cnt[4]++;   }     /*3. 打印结果*/   printf(“空格:%d\n”,cnt[0]);   printf(“小写:%d\n”,cnt[1]);   printf(“大写:%d\n”,cnt[2]);   printf(“数字:%d\n”,cnt[3]);   printf(“其他:%d\n”,cnt[4]);   return 0;  } 复制代码

复制2. 字符串排序

复制复制示例: #include //标准输入输出 #include //字符串处理头文件 ​ int main(int argc,char **argv) { int len=0; int i,j; char tmp; char str[100]; fgets(str,100,stdin); //从键盘上录入字符串 (标准输入) /*1. 计算字符串的长度*/ len=strlen(str); //计算字符串长度 /*2. 字符串排序*/ for(i=0;i;i++)>

复制复制3. 字符串插入

复制复制字符串插入: “1234567890” 在第2个位置后面插入”ABC” 最终结果: “12ABC34567890”

复制复制复制#include //标准输入输出 #include //字符串处理头文件 int main(int argc,char **argv) { int i,j; int src_len; int new_len; /* 123456789 12 3456789 */ char src_str[100]=”123456789″; char new_str[]=”abcd”; int addr=2; //插入的位置 /*1. 计算字符串的长度*/ src_len=strlen(src_str); //”123″ new_len=strlen(new_str); /*2. 字符串移动*/ for(i=src_len-1;i>addr-1;i–) { src_str[i+new_len]=src_str[i]; //向后移动 new_len } /*3. 插入新的数据*/ for(i=0;i;i++)src_str[addr+i]=new_str[i];>

复制复制复制5. 字符串删除

复制复制复制字符串删除: “1234567890” 删除”456” 最终结果: “1237890”

复制复制复制复制示例: #include //标准输入输出 #include //字符串处理头文件 ​ int main(int argc,char **argv) { char src_str[100]; char del_str[10]; int src_len=0,del_len=0; int i,j; int cnt=0; /*1. 录入字符串*/ printf(“输入源字符串:”); //123dufvdfv123dfljvb fgets(src_str,100,stdin); //从键盘上录入源字符串 printf(“输入查找的字符串:”); //123 fgets(del_str,10,stdin); //从键盘上录入源字符串 /*2. 计算长度*/ src_len=strlen(src_str); src_str[src_len-1]=\0; src_len-=1; //src_len=src_len-1; del_len=strlen(del_str); //”123\n” =4 del_str[del_len-1]=\0; del_len-=1; printf(“源字符串:%s,%d\n”,src_str,src_len); printf(“删除字符串:%s,%d\n”,del_str,del_len); ​ /*3. 查找*/ for(i=0;i+1;i++)>

复制复制复制6. 字符串替换

复制复制复制复制字符串”1234567890” 将456替换为”888” 最终: “1238887890” 需要考虑3种情况 复制代码

复制复制7. 字符串转整数。

复制复制从键盘上输入一个字符串”12345”, 得到整数: 12345;

复制复制复制#include //标准输入输出 #include //字符串处理头文件 int string_to_int(char str[]); int main(int argc,char **argv) { int data; char str[]=”125abcd”; data=string_to_int(str); printf(“data=%d\n”,data); return 0; } ​ /* 函数功能: 字符串转为整数 字符转为整数: -48 或者 -0 ​ 1234 */ int string_to_int(char str[]) { int value=0; //存放转换之后的结果 int i=0; while((str[i]!=\0)&&(str[i]>=0&&str[i]<=9))   {   value*=10;   value+=str[i]-0;   i++;   }   return value;  } 复制代码

复制复制四、GPS解码示例-字符串处理

复制复制复制#include #include typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; const u8 GPS_Info_1[]=”\ $GNGGA,114955.000,2842.4158,N,11549.5439,E,1,05,3.8,54.8,M,0.0,M,,*4F\ $GNGLL,2842.4158,N,11549.5439,E,114955.000,A,A*4D\ $GPGSA,A,3,10,31,18,,,,,,,,,,5.7,3.8,4.2*37\ $BDGSA,A,3,07,10,,,,,,,,,,,5.7,3.8,4.2*2A\ $GPGSV,3,1,10,10,49,184,42,12,16,039,,14,54,341,,18,22,165,23*7B\ $GPGSV,3,2,10,22,11,318,,25,51,055,,26,24,205,,29,13,110,*7C\ $GPGSV,3,3,10,31,50,287,36,32,66,018,*7F\ $BDGSV,1,1,04,03,,,07,05,,,29,07,79,246,33,10,52,232,19*62\ $GNRMC,114955.000,A,2842.4158,N,11549.5439,E,0.00,44.25,061117,,,A*4D\ $GNVTG,44.25,T,,M,0.00,N,0.00,K,A*14\ $GNZDA,114955.000,06,11,2017,00,00*47\ $GPTXT,01,01,01,ANTENNA OK*35″; const u8 GPS_Info_2[]=”\ $GPGSV,3,2,10,20,16,138,,21,27,204,,24,39,040,26,25,35,145,22*7B\ $GPGSV,3,3,10,31,13,226,20,32,33,300,23*7F\ $BDGSV,2,1,08,03,,,31,04,,,31,06,65,033,24,10,,,31*54\ $BDGSV,2,2,08,11,,,30,12,,,30,13,,,29,15,,,31*6C\ $GNRMC,144513.000,A,2856.3560,N,11524.5587,E,1.48,292.74,230817,,,A*7B\ $GNVTG,292.74,T,,M,1.48,N,2.73,K,A*22\ $GNZDA,144513.000,23,08,2017,00,00*43\ $GPTXT,01,01,01,ANTENNA OK*35\ $GNGGA,144514.000,2856.3563,N,11524.5596,E,1,06,4.0,-13.0,M,0.0,M,,*68\ $GNGLL,2856.3563,N,11524.5596,E,144514.000,A,A*40\ $GPGSA,A,3,15,24,12,18,25,,,,,,,,6.7,4.0,5.3*3E\ $BDGSA,A,3,06,,,,,,,,,,,,6.7,4.0,5.3*26\ “; #pragma pack(1) /* 必须在结构体定义之前使用,这是为了让结构体中各成员按1字节对齐 */ //美国GPS卫星信息 typedef struct { u8 num; //卫星编号 u8 eledeg; //卫星仰角 u16 azideg; //卫星方位角 u8 sn; //信噪比 }nmea_slmsg; //北斗卫星信息 typedef struct { u8 beidou_num; //卫星编号 u8 beidou_eledeg; //卫星仰角 u16 beidou_azideg; //卫星方位角 u8 beidou_sn; //信噪比 }beidou_nmea_slmsg; //UTC时间信息 typedef struct { u16 year; //年份 u8 month; //月份 u8 date; //日期 u8 hour; //小时 u8 min; //分钟 u8 sec; //秒钟 }nmea_utc_time; //GPS协议解析后数据存放结构体 typedef struct { u8 svnum; //可见GPS卫星数 u8 beidou_svnum; //可见GPS卫星数 nmea_slmsg slmsg[12]; //最多12颗GPS卫星 beidou_nmea_slmsg beidou_slmsg[12]; //暂且算最多12颗北斗卫星 nmea_utc_time utc; //UTC时间 u32 latitude; //纬度 分扩大100000倍,实际要除以100000 u8 nshemi; //北纬/南纬,N:北纬;S:南纬 u32 longitude; //经度 分扩大100000倍,实际要除以100000 u8 ewhemi; //东经/西经,E:东经;W:西经 u8 gpssta; //GPS状态:0,未定位;1,非差分定位;2,差分定位;6,正在估算. u8 posslnum; //用于定位的GPS卫星数,0~12. u8 possl[12]; //用于定位的卫星编号 u8 fixmode; //定位类型:1,没有定位;2,2D定位;3,3D定位 u16 pdop; //位置精度因子 0~500,对应实际值0~50.0 u16 hdop; //水平精度因子 0~500,对应实际值0~50.0 u16 vdop; //垂直精度因子 0~500,对应实际值0~50.0 int altitude; //海拔高度,放大了10倍,实际除以10.单位:0.1m u16 speed; //地面速率,放大了1000倍,实际除以10.单位:0.001公里/小时 }GPS_Msg; u8 GPS_GetCommaOffset(u8 *buf,u8 cnt); void GPS_MsgShow(void); u8 GPS_GetCommaOffset(u8 *buf,u8 cnt); void GPS_GPGSV_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf); void GPS_GNVTG_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf); void GPS_GNRMC_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf); void GPS_GNGSA_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf); void GPS_BDGSV_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf); void GPS_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf); GPS_Msg GPS_DecodeInfo; //存放GPS解码信息 int main() { GPS_InfoGet(&GPS_DecodeInfo,GPS_Info_2); GPS_MsgShow(); return 0; } const u8*fixmode_tbl[4]={“失败”,”失败”,” 2D “,” 3D “}; //fix mode字符串 u8 dtbuf[50]; //打印缓存器 /* 函数功能:显示GPS定位信息 */ void GPS_MsgShow(void) { float tp; tp=GPS_DecodeInfo.longitude; sprintf((char *)dtbuf,”经度:%.5f %1c”,tp/=100000,GPS_DecodeInfo.ewhemi); //得到经度字符串 printf(“%s\r\n”,dtbuf); tp=GPS_DecodeInfo.latitude; sprintf((char *)dtbuf,”纬度:%.5f %1c”,tp/=100000,GPS_DecodeInfo.nshemi); //得到纬度字符串 printf(“%s\r\n”,dtbuf); tp=GPS_DecodeInfo.altitude; sprintf((char *)dtbuf,”高度:%.1fm”,tp/=10); //得到高度字符串 printf(“%s\r\n”,dtbuf); tp=GPS_DecodeInfo.speed; sprintf((char *)dtbuf,”速度:%.3fkm/h”,tp/=1000); //得到速度字符串 printf(“%s\r\n”,dtbuf); if(GPS_DecodeInfo.fixmode<=3) //定位状态 { sprintf((char *)dtbuf,”定位模式:%s”,fixmode_tbl[GPS_DecodeInfo.fixmode]); printf(“%s\r\n”,dtbuf); } sprintf((char *)dtbuf,”GPS+BD 定位的GPS卫星数:%02d”,GPS_DecodeInfo.posslnum); //用于定位的GPS卫星数 printf(“%s\r\n”,dtbuf); sprintf((char *)dtbuf,”GPS 可见GPS卫星数:%02d”,GPS_DecodeInfo.svnum%100); //可见GPS卫星数 printf(“%s\r\n”,dtbuf); sprintf((char *)dtbuf,”BD 可见北斗卫星数:%02d”,GPS_DecodeInfo.beidou_svnum%100); //可见北斗卫星数 printf(“%s\r\n”,dtbuf); sprintf((char *)dtbuf,”UTC日期:%04d/%02d/%02d “,GPS_DecodeInfo.utc.year,GPS_DecodeInfo.utc.month,GPS_DecodeInfo.utc.date); //显示UTC日期 printf(“%s\r\n”,dtbuf); sprintf((char *)dtbuf,”显示UTC时间:%02d:%02d:%02d “,GPS_DecodeInfo.utc.hour,GPS_DecodeInfo.utc.min,GPS_DecodeInfo.utc.sec); //显示UTC时间 printf(“%s\r\n”,dtbuf); } /* 函数功能:从buf里面得到第cnt个逗号所在的位置 返 回 值:0~254,代表逗号所在位置的偏移. 255,代表不存在第cnt个逗号 */ u8 GPS_GetCommaOffset(u8 *buf,u8 cnt) { u8 *p=buf; while(cnt) { if(*buf==*||*buf< ||*buf>z)return 255;//遇到*或者非法字符,则不存在第cx个逗号 if(*buf==,)cnt–; buf++; } return buf-p; //计算偏移量 } /* 函数功能:m^n函数 返 回 值:m^n次方. */ u32 GPS_GetPow(u8 m,u8 n) { u32 tmp=1; while(n–)tmp*=m; return tmp; } /* 函数功能:str转换为数字,以,或者*结束 函数参数:buf:数字存储区 dx:小数点位数,返回给调用函数 返 回 值:转换后的整数数值 */ int GPS_StrtoNum(u8 *buf,u8*dx) { u8 *p=buf; u32 ires=0,fres=0; u8 ilen=0,flen=0,i; u8 mask=0; int res; while(1) //得到整数和小数的长度 { if(*p==-){ mask|=0X02; p++; }//是负数 if(*p==,||(*p==*))break;//遇到结束了 if(*p==.){ mask|=0X01; p++; }//遇到小数点了 else if(*p>9||(*p<0)) //有非法字符 { ilen=0; flen=0; break; } if(mask&0X01)flen++; else ilen++; p++; } if(mask&0X02)buf++; //去掉负号 for(i=0; i5)flen=5; //最多取5位小数 *dx=flen; //小数点位数 for(i=0; isvnum=GPS_StrtoNum(p1+posx,&dx); for(i=0; islmsg[slx].num=GPS_StrtoNum(p1+posx,&dx); //得到卫星编号 else break; posx=GPS_GetCommaOffset(p1,5+j*4); if(posx!=0XFF)GPS_DecodeInfo->slmsg[slx].eledeg=GPS_StrtoNum(p1+posx,&dx);//得到卫星仰角 else break; posx=GPS_GetCommaOffset(p1,6+j*4); if(posx!=0XFF)GPS_DecodeInfo->slmsg[slx].azideg=GPS_StrtoNum(p1+posx,&dx);//得到卫星方位角 else break; posx=GPS_GetCommaOffset(p1,7+j*4); if(posx!=0XFF)GPS_DecodeInfo->slmsg[slx].sn=GPS_StrtoNum(p1+posx,&dx); //得到卫星信噪比 else break; slx++; } p=p1+1;//切换到下一个GPGSV信息 } } /* 函数功能:分析BDGSV信息 函数参数:GPS_DecodeInfo:nmea信息结构体 buf:接收到的GPS数据缓冲区首地址 */ void GPS_BDGSV_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf) { u8 *p,*p1,dx; u8 len,i,j,slx=0; u8 posx; p=buf; p1=(u8*)strstr((const char *)p,”$BDGSV”); if(!p1)return; //没有查找成功 len=p1[7]-0; //得到BDGSV的条数 posx=GPS_GetCommaOffset(p1,3); //得到可见北斗卫星总数 if(posx!=0XFF)GPS_DecodeInfo->beidou_svnum=GPS_StrtoNum(p1+posx,&dx); for(i=0; ibeidou_slmsg[slx].beidou_num=GPS_StrtoNum(p1+posx,&dx); //得到卫星编号 else break; posx=GPS_GetCommaOffset(p1,5+j*4); if(posx!=0XFF)GPS_DecodeInfo->beidou_slmsg[slx].beidou_eledeg=GPS_StrtoNum(p1+posx,&dx);//得到卫星仰角 else break; posx=GPS_GetCommaOffset(p1,6+j*4); if(posx!=0XFF)GPS_DecodeInfo->beidou_slmsg[slx].beidou_azideg=GPS_StrtoNum(p1+posx,&dx);//得到卫星方位角 else break; posx=GPS_GetCommaOffset(p1,7+j*4); if(posx!=0XFF)GPS_DecodeInfo->beidou_slmsg[slx].beidou_sn=GPS_StrtoNum(p1+posx,&dx); //得到卫星信噪比 else break; slx++; } p=p1+1;//切换到下一个BDGSV信息 } } /* 函数功能:分析GNGGA信息 函数参数: GPS_DecodeInfo:nmea信息结构体 buf:接收到的GPS数据缓冲区首地址 */ void GPS_GNGGA_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf) { u8 *p1,dx; u8 posx; p1=(u8*)strstr((const char *)buf,”$GNGGA”); if(!p1)return; //没有查找成功 posx=GPS_GetCommaOffset(p1,6); //得到GPS状态 if(posx!=0XFF)GPS_DecodeInfo->gpssta=GPS_StrtoNum(p1+posx,&dx); posx=GPS_GetCommaOffset(p1,7); //得到用于定位的卫星数 if(posx!=0XFF)GPS_DecodeInfo->posslnum=GPS_StrtoNum(p1+posx,&dx); posx=GPS_GetCommaOffset(p1,9); //得到海拔高度 if(posx!=0XFF)GPS_DecodeInfo->altitude=GPS_StrtoNum(p1+posx,&dx); } /* 函数功能:分析GNGSA信息 参 数:GPS_DecodeInfo:nmea信息结构体 buf:接收到的GPS数据缓冲区首地址 */ void GPS_GNGSA_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf) { u8 *p1,dx; u8 posx; u8 i; p1=(u8*)strstr((const char *)buf,”$GNGSA”); if(!p1)return; //没有查找成功 posx=GPS_GetCommaOffset(p1,2); //得到定位类型 if(posx!=0XFF)GPS_DecodeInfo->fixmode=GPS_StrtoNum(p1+posx,&dx); for(i=0; i<12; i++) //得到定位卫星编号 { posx=GPS_GetCommaOffset(p1,3+i); if(posx!=0XFF)GPS_DecodeInfo->possl[i]=GPS_StrtoNum(p1+posx,&dx); else break; } posx=GPS_GetCommaOffset(p1,15); //得到PDOP位置精度因子 if(posx!=0XFF)GPS_DecodeInfo->pdop=GPS_StrtoNum(p1+posx,&dx); posx=GPS_GetCommaOffset(p1,16); //得到HDOP位置精度因子 if(posx!=0XFF)GPS_DecodeInfo->hdop=GPS_StrtoNum(p1+posx,&dx); posx=GPS_GetCommaOffset(p1,17); //得到VDOP位置精度因子 if(posx!=0XFF)GPS_DecodeInfo->vdop=GPS_StrtoNum(p1+posx,&dx); } /* 函数功能:分析GNRMC信息 函数参数:GPS_DecodeInfo:nmea信息结构体 buf:接收到的GPS数据缓冲区首地址 */ void GPS_GNRMC_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf) { u8 *p1,dx; u8 posx; u32 temp; float rs; p1=(u8*)strstr((const char *)buf,”$GNRMC”);//”$GNRMC”,经常有&和GNRMC分开的情况,故只判断GPRMC. if(!p1)return; //没有查找成功 posx=GPS_GetCommaOffset(p1,1); //得到UTC时间 if(posx!=0XFF) { temp=GPS_StrtoNum(p1+posx,&dx)/GPS_GetPow(10,dx); //得到UTC时间,去掉ms GPS_DecodeInfo->utc.hour=temp/10000; GPS_DecodeInfo->utc.min=(temp/100)%100; GPS_DecodeInfo->utc.sec=temp%100; } posx=GPS_GetCommaOffset(p1,3); //得到纬度 if(posx!=0XFF) { temp=GPS_StrtoNum(p1+posx,&dx); GPS_DecodeInfo->latitude=temp/GPS_GetPow(10,dx+2); //得到° rs=(float)(temp%GPS_GetPow(10,dx+2)); //得到 GPS_DecodeInfo->latitude=(u32)(GPS_DecodeInfo->latitude*GPS_GetPow(10,5)+(rs*GPS_GetPow(10,5-dx))/60);//转换为° } posx=GPS_GetCommaOffset(p1,4); //南纬还是北纬 if(posx!=0XFF)GPS_DecodeInfo->nshemi=*(p1+posx); posx=GPS_GetCommaOffset(p1,5); //得到经度 if(posx!=0XFF) { temp=GPS_StrtoNum(p1+posx,&dx); GPS_DecodeInfo->longitude=temp/GPS_GetPow(10,dx+2); //得到° rs=(float)(temp%GPS_GetPow(10,dx+2)); //得到 GPS_DecodeInfo->longitude=(u32)(GPS_DecodeInfo->longitude*GPS_GetPow(10,5)+(rs*GPS_GetPow(10,5-dx))/60);//转换为° } posx=GPS_GetCommaOffset(p1,6); //东经还是西经 if(posx!=0XFF)GPS_DecodeInfo->ewhemi=*(p1+posx); posx=GPS_GetCommaOffset(p1,9); //得到UTC日期 if(posx!=0XFF) { temp=GPS_StrtoNum(p1+posx,&dx); //得到UTC日期 GPS_DecodeInfo->utc.date=temp/10000; GPS_DecodeInfo->utc.month=(temp/100)%100; GPS_DecodeInfo->utc.year=2000+temp%100; } } /* 函数功能:分析GNVTG信息 函数参数:GPS_DecodeInfo:nmea信息结构体 buf:接收到的GPS数据缓冲区首地址 */ void GPS_GNVTG_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf) { u8 *p1,dx; u8 posx; p1=(u8*)strstr((const char *)buf,”$GNVTG”); if(!p1)return; //没有查找成功 posx=GPS_GetCommaOffset(p1,7); //得到地面速率 if(posx!=0XFF) { GPS_DecodeInfo->speed=GPS_StrtoNum(p1+posx,&dx); if(dx<3)GPS_DecodeInfo->speed*=GPS_GetPow(10,3-dx); //确保扩大1000倍 } } /* 函数功能:提取GPS信息 函数参数:GPS_DecodeInfo:nmea信息结构体 buf:接收到的GPS数据缓冲区首地址 */ void GPS_InfoGet(GPS_Msg *GPS_DecodeInfo,u8 *buf) { GPS_GPGSV_InfoGet(GPS_DecodeInfo,buf); //GPGSV解析-OK GPS_BDGSV_InfoGet(GPS_DecodeInfo,buf); //BDGSV解析-OK GPS_GNGGA_InfoGet(GPS_DecodeInfo,buf); //GNGGA解析-OK GPS_GNGSA_InfoGet(GPS_DecodeInfo,buf); //GPNSA解析-ON GPS_GNRMC_InfoGet(GPS_DecodeInfo,buf); //GPNMC解析-OK GPS_GNVTG_InfoGet(GPS_DecodeInfo,buf); //GPNTG解析-OK };>;>;>;>

复制复制

;i++)src_str[addr+i]=new_str[i];>

猜你喜欢