c - fscanf bus error: 10 when switching from Snow Leopard to Lion -
first off, snippet not meant production code. please, no lecturing "being unsafe." thanks!
so, following code part of parser takes in csv , uses populate sqlite3 db. when compiled , ran in snow leopard, worked fine. i've switched lion, scanf statement throws bus error: 10. specifically, seems have how consuming , discarding '\n' @ end of each line:
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... ...
if remove last %*c, meant consume '\n' , ignore advance next line, program not crash. of course incorrect parsing.
also, mind you, eof doesn't seem problem; i'e tried single fscanf statement instead of while-loop shown above.
any thoughts?
edit: let me add code compiled , ran in intel core duo (32-bit) macbook snow leopard , compiling , running on macpro (64-bit) lion. wonder if might have alignment?
interesting. bus errors due alignment issues may not case here since you're scanning in char
s.
one thing may want consider fgets
entire line buffer , sscanf
it. allow 2 things:
- print out line in debug statement before
sscanf
ing (or after scanning, if expected conversion count wrong), can see if there problems; and - not worry trying align line-ending
fscanf
, sincefgets
job of already.
so (untested):
char bighonkinbuffer[16384]; while (fgets (bighonkinbuffer, sizeof(bighonkinbuffer), pfile) != null) { if (sscanf(bighonkinbuffer, "%[^,],%[^,],%[^,],%[^,],%[^\n]", name, country, last,first, photouri) != 5) { // printf ("not scanned properly: [%s]\n", bighonkinbuffer); exit (1); } }
you should check return values sqlite3_open
, fopen
calls, if more "play" code (i.e., if there's slightest possibility files may not exist).
Comments
Post a Comment