【发布时间】:2024-01-08 13:38:01
【问题描述】:
我有一个 vb6 程序,它从 sql server 读取数据并将它们写入文本文件。每条记录由换行符分隔。 这些文件(也可以大于 200mb)必须在 sqlite 数据库中从 iPad 读取和写入。 为了避免内存警告,我在 C 中使用此函数读取文件的每一行
“strRet”是C中读取的字符串
“NSString *stringa”是字符串C转化为NSString
NSDictionary *readLineAsNSString(FILE *f,int pospass,BOOL testata,int primorecord )
{
char *strRet = malloc(BUFSIZ);//(char *) togliere perche con c potrebbe restituire un int
if (strRet==NULL)
{
return nil;
}
int size = BUFSIZ;
BOOL finito=NO;
int pos = 0;
int c;
fseek(f,pospass,SEEK_SET);
do{ // read one line
c = fgetc(f);
if (pos >= size-1)
{
size=size+BUFSIZ;
strRet = realloc(strRet, size);
if (strRet==NULL)
{
return nil;
}
}
if(c != EOF)
{
strRet[pos] = c;
pos=pos+1;
}
else
{
finito=YES;
}
}while(c != EOF && c != '\n');
if (pos!=0)
{
strRet[pos] = '\0';
}
NSString *stringa=[NSString stringWithCString:strRet encoding:NSASCIIStringEncoding];
if (pos==0)
{
stringa=@"";
}
long long sizerecord;
if (pos!=0)
{
sizerecord= (long long) [[NSString stringWithFormat:@"%ld",sizeof(char)*(pos)] longLongValue];
}
else
{
sizerecord=0;
}
pos = pospass + pos;
NSDictionary *risultatoc = @{st_risultatofunzione: stringa,
st_criterio: [NSString stringWithFormat:@"%d",pos],
st_finito: [NSNumber numberWithBool:finito],
st_size: [NSNumber numberWithLongLong: sizerecord]
};
//Svuoto il buffer
free(strRet);
// free(tmpStr);
strRet=NULL;
return risultatoc;
}
但是,当我在文件中有特殊字符(例如 € 符号或重音字母或某些北欧国家)时,记录无法正确读取,并且我发现自己带有随机字符的 NSString而不是正确的。 你知道你帮我吗?谢谢!
【问题讨论】:
-
在
stringWithCString:encoding方法中,尝试将encoding更改为NSWindowsCP1252StringEncoding。见madore.org/~david/computers/unicode/cstab.html#CP1252
标签: ios c text ascii non-ascii-characters