Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
fscanf总线错误:从雪豹切换到狮子时为10_C_Macos_Memory Management_Osx Lion_Scanf - Fatal编程技术网

fscanf总线错误:从雪豹切换到狮子时为10

fscanf总线错误:从雪豹切换到狮子时为10,c,macos,memory-management,osx-lion,scanf,C,Macos,Memory Management,Osx Lion,Scanf,首先,这个代码段不是用于生产代码的。所以,请不要说“不安全”,谢谢 因此,下面的代码是接受csv并使用它填充SQLite3DB的解析器的一部分。当在SnowLeopard中编译和运行时,它运行得很好。现在我已经切换到Lion,scanf语句抛出总线错误:10。具体来说,这似乎与我如何消费和丢弃每行末尾的“\n”有关: int main() { sqlite3* db; sqlite3_open("someExistingDB.sqlite3", &db); FILE *pF

首先,这个代码段不是用于生产代码的。所以,请不要说“不安全”,谢谢

因此,下面的代码是接受csv并使用它填充SQLite3DB的解析器的一部分。当在SnowLeopard中编译和运行时,它运行得很好。现在我已经切换到Lion,scanf语句抛出总线错误:10。具体来说,这似乎与我如何消费和丢弃每行末尾的“\n”有关:

int main()
{
  sqlite3* db;
  sqlite3_open("someExistingDB.sqlite3", &db);

  FILE *pFile;
  pFile = fopen("excelData.csv","r");

  char name[256],country[256], last[256], first[256], photoURI[256];
  char sqlStatement[16384];

  while(fscanf(pFile, "%[^,],%[^,],%[^,],%[^,],%[^\n]%*c", name, country, last,first, photoURI) != EOF)
  {
          blah...

   ...
如果我删除最后一个%*c,这意味着要使用“\n”并忽略它以便前进到下一行,则程序不会崩溃。当然,这是一个错误的解析

另外,请注意,EOF似乎不是问题;我还尝试了一个fscanf语句,而不是上面所示的while循环

有什么想法吗


编辑:让我补充一点,这段代码最初是在带有雪豹的intel core duo(32位)macbook上编译和运行的,现在我正在编译它并在带有Lion的MacPro(64位)上运行。所以我想知道这是否与对齐有关?

有趣。总线错误通常是由于对齐问题造成的,但这里可能不是这种情况,因为您所扫描的是
char
s

您可能要考虑的是:<代码> fgs<代码>整个行进入缓冲区和<代码> sSCANF它。这将允许您做两件事:

  • sscanf
    ing之前(或者在扫描之后,如果预期的转换计数错误),在调试语句中打印该行,以便查看是否存在任何问题;及
  • 不用担心尝试对齐以
    fscanf
    结尾的行,因为
    fgets
    已经很好地完成了这项工作
因此,它将类似于(未经测试):


您还应该检查
sqlite3\u open
fopen
调用的返回值,如果这不仅仅是“播放”代码(即,如果这些文件可能根本不存在的话)。

我在Mac Mini running Lion(10.7.1)上用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;
}
没有任何形式的警告。给定输入数据:

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>>
名称:
国家:
最后:
第一:
photoURI:
姓名:
国家:
最后:
第一:
photoURI:
姓名:
国家:

最后:这可能在几个地方出错;我建议检查
fopen(3)
sqlite3\u open()
是否成功,从
fscanf(3)
打印实际返回值(可能它返回了一个
0
的早期匹配失败?)。另外,也可以尝试将
%*c
替换为“`”(单个空格),因为输入规范中的任何空格都会占用所有空格,直到下一个非空格字符。请注意,
fscanf
可以返回-1到5之间的任意位置,具体取决于成功读取的项目数。如果它的读数介于0和4之间,字符串中将有垃圾剩余,这可能会导致总线错误。
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>>