我想你的代码的以下适应上的Mac Mini与的XCode 4.
#include <stdio.h>
static void print(const char *tag, const char *str)
{
printf("%8s: <<%s>>\n", tag, str);
}
int main(void)
{
FILE *pFile = fopen("excelData.csv","r");
char name[256], country[256], last[256], first[256], photoURI[256];
while (fscanf(pFile, "%[^,],%[^,],%[^,],%[^,],%[^\n]%*c",
name, country, last, first, photoURI) == 5)
{
print("name", name);
print("country", country);
print("last", last);
print("first", first);
print("photoURI", photoURI);
}
return 0;
}
我公司生产的64位二进制使用运行狮(10.7.1):
gcc -O -std=c99 -Wall -Wextra xxx.c -o xxx
没有任何警告。由于输入数据:
Monster,United States,Smith,John,http://www.example.com/photo1
Emancipated Majority,Canada,Jones,Alan,http://www.example.com/photo2
A Much Longer Name Than Any Before,A Land from Far Away and In the Imagination Most Beautiful,OneOfTheLongerFamilyNamesYou'llEverSee,ALongishGivenName,http://www.example.com/photo3/elephant/pygmalion/photo3,x31
它产生的输出:
name: <<Monster>>
country: <<United States>>
last: <<Smith>>
first: <<John>>
photoURI: <<http://www.example.com/photo1>>
name: <<Emancipated Majority>>
country: <<Canada>>
last: <<Jones>>
first: <<Alan>>
photoURI: <<http://www.example.com/photo2>>
name: <<A Much Longer Name Than Any Before>>
country: <<A Land from Far Away and In the Imagination Most Beautiful>>
last: <<OneOfTheLongerFamilyNamesYou'llEverSee>>
first: <<ALongishGivenName>>
photoURI: <<http://www.example.com/photo3/elephant/pygmalion/photo3,x31>>
的!= EOF
VS == 5
变化不会与样本数据重要,但可以说是更稳健的总称。最后一行数据利用您的模式变化,并在'最后一个字段'中包含逗号。
由于您的代码没有检查文件是否正确打开,所以我不得不怀疑这是否是您的问题,尽管这可能会产生分割冲突而不是总线错误。
所以,没有回答你的问题 - 但有些代码让你试试。
这可能会在几个地方出错;我建议检查'fopen(3)'和'sqlite3_open()'是否成功,从'fscanf(3)'打印实际返回值(也许它返回一个'0'来表示早期匹配失败?)。另外,也许尝试用''(单个空格)替换'%* c',因为输入规范中的任何空白都会消耗所有空白,直到下一个非空白字符。 – sarnold
请注意,'fscanf'可以返回-1和5之间的任何位置,具体取决于成功读取了多少项。如果它读取0和4之间的数字,则会在字符串中留下垃圾,这可能会导致总线错误。 –