本文介绍了如何以比此代码更快的速度读取文件,通过更改可以使用类似的缓冲区技术或其他比此更快的速度.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
FILE* fp;
fp = fopen(strFilePath, "r");
char s1[651];
int iCount = 0;
bool stx_found = false;
while(!feof(fp))
{
CASSImportRow structCASSImport;
if(fgets(s1, 651, fp) != NULL)
{
if(s1[0] == ''\n'')
continue;
CString tempRecord = s1;
if (address_seq_perfix.Compare("STX") != 0)
{
continue;
}
stx_found = true;
structCASSImport.strRegNo = CORBA::string_dup(tempRecord.Mid(3 , 12));
structCASSImport.strADDR = CORBA::string_dup((tempRecord.Mid(92 , 35)).TrimRight());
structCASSImport.strCity = CORBA::string_dup((tempRecord.Mid(127, 20)).TrimRight());
structCASSImport.strCountry = CORBA::string_dup((tempRecord.Mid(161, 20)).TrimRight());
structCASSImport.strState = CORBA::string_dup((tempRecord.Mid(445, 2 )).TrimRight());
structCASSImport.strReturnedZIP = CORBA::string_dup(tempRecord.Mid(447, 5 ));
structCASSImport.strReturnedZIPExt = CORBA::string_dup(tempRecord.Mid(452, 4 ));
structCASSImport.strReturnedDPC = CORBA::string_dup(tempRecord.Mid(456, 2 ));
structCASSImport.strRCHK = CORBA::string_dup(tempRecord.Mid(458, 1 ));
iCount++;
(*ptrCASSImportIn).length(iCount);
(*ptrCASSImportIn)[iCount-1] = structCASSImport;
}
}
fclose(fp);
推荐答案
FILE* fp = fopen(strFilePath, "r");
char *buffer = malloc(1048576); // Allocate a 1Mb buffer
int read = fread(buffer,1,1048576,fp);
然后扫描内存缓冲区中的换行符以逐行处理它.
and then scan the memory buffer for newline characters to process it line by line.
char sl[651];
int slcounter = 0;
for(int i=0;i<read;i++){
if(buffer[i] != ''\n''){
sl[slcounter] = buffer[i];
slcounter++;
}
else{
sl[slcounter] = 0;
slcounter = 0; // reset the character counter for the next string
// do your regular processing of sl here
}
}
如果您的文件大于1Mb,请调整缓冲区大小或进行更多次读取,直到到达文件末尾.
If your file is bigger than 1Mb, adjust the buffer size or do more freads until the end of file is reached.
这篇关于如何以比此代码更快的速度读取文件,通过更改可以使用类似的缓冲区技术或其他比此更快的速度.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!