strtol 不会转换我从阅读文本文件中获得的字符串。
int getNumFiles(int archive){
off_t cur=lseek(archive,20,SEEK_SET);
if(cur==-1){
printf("lseek ERROR\n");
exit(-1);
}
bool b=true;
char headerSizeBuffer[4];
char *end;
while(b){
int numRead=read(archive,headerSizeBuffer,3);
if(numRead != 3){
printf("read ERROR\n");
exit(-1);
}
headerSizeBuffer[3]='\0';
printf("headerSizeBuffer=%s with length=%ld\n",headerSizeBuffer,strlen(headerSizeBuffer));
long headerSize=strtol(headerSizeBuffer,&end,10);//atol(headerSizeBuffer);
printf("headerSize=%ld\n",headerSize);
if (!*end)
printf("Converted successfully\n");
else
printf("Conversion error, non-convertible part: %s\n", end);
b=false;
}
return 1;
}
当我运行编译的代码时,控制台给了我这个
headerSizeBuffer=031l_archive with length=12
headerSize=31
Conversion error, non-convertible part: l_archive
我要做的就是将 031 转换为值为 31 的 long 或 int。