1.在循环使用数组时需要清理数组
CString str;
memset(str,0,strlen(str));
判断两个字符串包含数字大小是否相等
CString str="22";
if(str!="22"){}
2.读取文件
//2.读人员文件renyuan.txt HANDLE rFile; DWORD dwRet; memset(str,0,2000); rFile = CreateFile(_T("\NANDFlash\renyuan.txt"),GENERIC_READ ,0,NULL,OPEN_EXISTING,0,NULL); if(rFile == INVALID_HANDLE_VALUE) { AfxMessageBox(_T("打开失败")); return false; }else{ ReadFile(rFile,str,5000,&dwRet,NULL); ss=str; do{ index = ss.Find( ';' ); temp=ss.Left( index ) ; n = temp.Find( ':' ); renyuan_name[num_renyuan]=temp.Left(n); arr=temp.Right( temp.GetLength( )-n-1 ) ; m = arr.Find( ',' ); renyuan_gonghao[num_renyuan]=arr.Left(m); renyuan_danwei[num_renyuan]=arr.Right( arr.GetLength( )-m-1 ) ; ss=ss.Right( ss.GetLength( )-index-1 ); num_renyuan++; }while(ss.Find( ';' )!=-1); } CloseHandle(rFile);
或者
memset(str,0,2000); num_renyuan=0; hFile =file.Open(_T("\NANDFlash\renyuan.txt"),CFile::modeRead ); if(hFile == -1){ AfxMessageBox(_T("file open error ")); } file.Read(str, 2000); ss=str; do{ index = ss.Find( ';' ); temp=ss.Left( index ) ; n = temp.Find( ':' ); renyuan_name[num_renyuan]=temp.Left(n); arr=temp.Right( temp.GetLength( )-n-1 ) ; m = arr.Find( ',' ); renyuan_gonghao[num_renyuan]=arr.Left(m); renyuan_danwei[num_renyuan]=arr.Right( arr.GetLength( )-m-1 ) ; ss=ss.Right( ss.GetLength( )-index-1 ); num_renyuan++; }while(ss.Find( ';' )!=-1); file.Close();
写文件代码:
//写入数据 HANDLE hFile; DWORD dwRet; char str[2000]; hFile = CreateFile(_T("\NANDFlash\renyuan.txt"),GENERIC_READ | GENERIC_WRITE,0,NULL,TRUNCATE_EXISTING,0,NULL); if(hFile == INVALID_HANDLE_VALUE) { AfxMessageBox(_T("打开失败")); }else{ if(MessageBox(_T("确认删除用户,密码?"),_T("提示"),MB_OKCANCEL)==1) { int i,j; //1.先将数据更新到数组, for(i=0,j=0;i<num_renyuan;i++,j++) { if(i<flag||i>flag){ renyuan_name[j]=renyuan_name[i]; renyuan_gonghao[j]=renyuan_gonghao[i]; renyuan_danwei[j]=renyuan_danwei[i]; }else if(i==flag){ i++; renyuan_name[j]=renyuan_name[i]; renyuan_gonghao[j]=renyuan_gonghao[i]; renyuan_danwei[j]=renyuan_danwei[i]; } } num_renyuan--; //2.然后将数据从数组更新到文件中 for(i=0;i<num_renyuan;i++) { CString temp;//将temp清零 temp+=renyuan_name[i]; temp+=":"; temp+=renyuan_gonghao[i]; temp+=","; temp+=renyuan_danwei[i]; temp+=";"; sprintf(str,"%S",temp);//S这样str就是完整的"username:passwors;",如果换成s,只会将一个字符传给str WriteFile(hFile, str, strlen(str),&dwRet,NULL); } CloseHandle(hFile); OnOK(); } }