JG+ 不定长查找 & 取字段
不定长查找
===========================
数据格式:"条码",数量
"123A",12
"1A",1
"12C37",9
"15h565",46
===========================
举例:扫描一个条码szCodeIn(如"12C37"),找到记录,数量减一,数量为0不保存
memset(szCmp,0,sizeof(szCmp));
memset(szTemp,0,sizeof(szTemp));
sprintf(szCmp,"\"%s\",",szCodeIn);
nExist=SearchFieldGr(szFile, szCmp, 1,1,',', 1,FORWARD,szTemp); //判断条码是否存在于文件
if(nExist>0)//存在
{
memset(szCount,0,sizeof(szCount));
strcpy(szCount,strchr(szTemp, ',')+1);//读取数量,存入szCount //用strchr找到第一个逗号
lnCurrentIn=atol(szCount)-1;//数量减一
DeleteRecord(szFile,nExist);//删除该笔记录
if(lnCurrentIn != 0L)
{
memset(szRecord,0,sizeof(szRecord));
sprintf(szRecord,"\"%s\",%ld",szCodeIn,lnCurrentIn);//"条码",数量
AppendRecord(szFile,szRecord);
}
}
计算总量,即把第2个字段的值取出后相加。若不为0,存入FILE_COUNT
FILE *fp;
int nNum,i;
long lnTotalIn;
fp=fopen(szFile,"rb");
if(fp==NULL)
{
fclose(fp);
//return 0;
}
else
{
fclose(fp);
lnTotalIn=0L;
nNum = GetRecordNum(szFile);
for(i=2;i<= nNum;i++)
{
memset(szTemp,0,sizeof(szTemp));
memset(szCount,0,sizeof(szCount));
strcat(szTemp,ReadRecord(szFile,i,0));
strcpy(szCount,strchr( szTemp, ',')+1);//读取数量
lnTotalIn+=atol(szCount);
}
if(lnTotalIn!=0L)
{
memset(szRecord,0,sizeof(szRecord));
sprintf(szRecord,"%s,%ld","in",lnTotalIn);
AppendRecord(FILE_COUNT,szRecord);
}
}
超过32位的不定长查找
要找的字段长度超过32位,不定长查找,代码参考
nRecordNum= GetRecordNum(ufilename);
memset(uAll2,0x0,sizeof(uAll2));
sprintf(uAll2,"%20s%s",uM1,_S1);//uAll2不定长,可能超过32位
nLen=strlen(uAll2);
if(nLen<32)
{
if(SearchField(ufilename, uAll2, 1, 1,',', 1, FORWARD|FULLMATCH) > 0)
{
//找到
}
else
{
//没找到
}
}
else
{
strncpy(szText,uAll2,31);
j=1;
n = nLen-31;
while(j<=nRecordNum)
{
i=0;
memset(szCmp,0x0,sizeof(szCmp));
i = SearchFieldGr(ufilename, szText, 1, 1,',', j, FORWARD,szCmp);
if(i > 0)
{
k=strlen(szCmp);
//printf(",%d,%d",k,i); cKey=_getc_ne();
if((k==nLen+1)&&(strncmp(szCmp+31,uAll2+31,n)==0))
{
//ok
}
else
{
//fail, next
j=i+1;
}
}
else
{
//没找到
break;
}
}
if(j > nRecordNum)
{
//没找到
}
}
SearchFieldGr 函数说明(最长可查32位)
int SearchFieldGr ( char* filename,
char* field,
int fieldno,
int totalfields,
char fielddlt,
int recordno,
int flag,
char* buffer );
Parameter Description
filename Data file name. If it has a value NULL, then use the default data file name.
field Specifies the key data to be searched.
fieldno The ordinal number of the searched field.
totalfield Total number of fields in a record.
Fielddlt Character that is used for field delimiter.
recordno Record number that specified the start point of search.
Flag FORWARD: the search will start from the specified record toward the end of data file.
BACKWARD: the search will start from the specified record backward to the beginning of data file.
FULLMATCH: the search will make a complete key string matching. This flag can be combined together with either FORWARD or BACKWARD.
Buffer Buffer to receive the data of the record.
Search the field column from a specified record in a specified direction in the data file. All records should have the same format, that is all records have the same number of fields, and all fields are separated by the same delimiter, and each record should start at the beginning of a line.
If a matched data is found, the matched record will be retrieved to buffer.
If the data file is so large and contains thousands records and hundreds kilobytes to search, and
SearchFieldGr is too slow, then another very fast searching method, binary search can be
considered to apply.
Return Values
Value Description
0 Search is unsuccessful.
Other value Record number that indicate the record contains the searched field.
buffer Whole matched record when key data is successfully found.
GetRecordNum 可以算不定长记录数
int GetRecordNum ( char* filename );
Parameter Description
filename Data file name. If name has a value NULL, then use the default data file name.
Get the total records number in the data file.
Return Values
The total number of the records.
                    
                
                
            
        
浙公网安备 33010602011771号