2018年6月28日工作中遇到的问题及其解决思路

本文探讨了在使用CString的GetBufferSetLength方法时可能遇到的问题,特别是当分配的缓冲区长度超过实际需求时导致的字符串截断现象。通过详细分析及两个具体的排查步骤,帮助读者理解这一常见错误并提供了解决方案。

在做港中银人民币跨境支付系统客户化时遇到的问题:

  问题:在使用CString的GetBufferSetLength()时没有深刻理解这个函数的原理,在GetBufferSetlengh(5005)时,比如说我所用的字符串需要5000个字节,此时如果我申请了5005个字节,则会在第5001个5002个5003个5004个5005个赋值为'\0',而字符串遇到'\0'就会结束,然后再用该字符串和其它字符串拼接的时候就会无法拼接,因为这个字符串在5001的时候是'\0',就是结束了。

解决思路:

     1.一个一个字节的对比,看是否加减有错,计算字符位置的时候有错,没有错误的话排除计算错误。

     2.打印日志,把对应的用十六进制打印出来。

  

#include<iostream> using namespace std; //你提交的代码在这里 int day(int a,int b,int c) { int d[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; if(a%4==0&&a%100!=0||a%400==0) d[1]=29; for(int i=0; i<b-1; i++) { c+=d[i]; } return c; } int day(int b,int a) { int d[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; if(a%4==0&&a%100!=0||a%400==0) d[1]=29; return d[b-1]; } class Date { public: Date():h(2023),s(20),m(5) {} //Rectangle (double a,double b):w(a),h(b){} Date(int a,int b,int c):h(a),m(b),s(c){} void Set(int a,int b,int c){h=a,m=b,s=c;} Date(const Date &a):s(a.s),h(a.h),m(a.m) {} int operator -(const Date&a) { if(h==a.h) return day(h,m,s)-day(a.h,a.m,a.s); if(h<a.h) { int b=0; for(int i=h+1; i<a.h; i++) b+=day(i,12,31); b+=day(a.h,a.m,a.s); return b-day(h,m,s); } if(h>a.h) { int b=0; for(int i=a.h+1; i<h; i++) b+=day(i,12,31); b+=day(h,m,s); return b-day(a.h,a.m,a.s); } } Date operator+(int n) { Date p(h,m,s); int count=0; for(p.h;;) { for(p.m; p.m<=12; p.m++) { for(p.s; p.s<=day(p.m,p.h); p.s++) { if(count==n) return Date(p.h,p.m,p.s); count++; } p.s=1; } p.m=1; p.h++; } } Date operator-(int n) { Date p(h,m,s); int count=0; if(p.s-1>=n) return Date(p.h,p.m,p.s-n); else { // count=1; p.m--; if(p.m==0) { p.m=12; p.h--; } count+=p.s; for(p.h;;) { for(p.m; p.m>=1; p.m--) { p.s=day(p.m,p.h); for(p.s=day(p.m,p.h); p.s>=1; p.s--) { if(count==n) return Date(p.h,p.m,p.s); count++; } } p.m=12; p.h--; } } } friend ostream& operator<<(ostream &a,const Date &b)//const Circle &b) { a<<b.h<<"/"<<b.m<<"/"<<b.s; return a; } friend istream& operator >>(istream&a,Date&b) { a>>b.h>>b.m>>b.s; return a; } bool operator >(const Date &a) { if(h>a.h) return true; else if(h==a.h) { if(day(h,m,s)>day(a.h,a.m,a.s)) return true; else return false; } else return false; } private: int h,m,s; }; int main() { Date d1; cout<<"Show object d1:"<<d1<<endl; int year,month,day; cin>>year>>month>>day; Date d2(year,month,day); cout<<"Show object d2:"<<d2<<endl; cin>>year>>month>>day; d1.Set(year,month,day); cout<<"Reset and Show object d1:"<<d1<<endl; int n; n=d2-d1; cout<<d2<<" - "<<d1<<" = "<<n<<endl; cin>>n; cout<<d1<<" + "<<n<<" = "; d2=d1+n; cout<<d2<<endl; cout<<d1<<" - "<<n<<" = "; d1=d1-n; cout<<d1<<endl; return 0; } /* 2023 1 5 2020 7 8 800 */
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值