| | |
Segmentation Fault error
Thread Solved
![]() |
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
Hi All,
Need our expertise out there regarding the above mention error and below are the codes:
When I run the program, it gives me "Segmentation Fault" and I think it's mayb becos of combining 2 tables.
Many thanks..
Need our expertise out there regarding the above mention error and below are the codes:
C Syntax (Toggle Plain Text)
/* send SQL query */ bzero(query1, sizeof(query1)); sprintf(query1, "select a.CreateDate, a.MobileNumber, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport," "b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Postcode, b.CountryCode from Register as a, Profile as b" " where a.MasterNumber = b.Number and a.Status=0"); printf ("%s\n",query1); if (mysql_query(&conn, query1)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } res = mysql_use_result(&conn); /* output fields 0, 1 and 2 of each row */ while ((row = mysql_fetch_row(res)) != NULL) { if (i==11) { timebuffer(); i=1; bzero(string1, sizeof(string1)); } else { sprintf(string1,"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s", row[0], row[1], row[14], row[15], row[16], row[17], row[18], row[4], row[5], row[6], row[7], row[12], row[13]); sprintf(mobileno,"%s", row[1]); printf ("%s\n",string1); printf ("Test: %s\n",mobileno); writeLogFile(string1, mobileno); i++; } }
Many thanks..
Last edited by whitemoss; Dec 4th, 2006 at 9:43 pm.
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
sorry..aku post again my codes:
C Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { char string1[1600], string2[1600], query[1600], query1[1600], mobileno[50]; int i, j, len; FILE *fp2; mysql_init(&conn); /* Connect to database */ if (!mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } /* send SQL query */ bzero(query1, sizeof(query1)); sprintf(query1, "select a.CreateDate, a.MobileNumber, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport," "b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Postcode, b.CountryCode from Register as a, Profile as b" " where a.MasterNumber = b.Number and a.Status=0"); printf ("%s\n",query1); if (mysql_query(&conn, query1)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } res = mysql_use_result(&conn); /* output fields 0, 1 and 2 of each row */ while ((row = mysql_fetch_row(res)) != NULL) { if (i==11) { timebuffer(); i=1; bzero(string1, sizeof(string1)); } else { sprintf(string1,"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s", row[0], row[1], row[14], row[15], row[16], row[17], row[18], row[4], row[5], row[6], row[7], row[12], row[13]); sprintf(mobileno,"%s", row[1]); printf ("%s\n",string1); printf ("Test: %s\n",mobileno); writeLogFile(string1, mobileno); i++; } /* Release memory used to store results and close connection */ mysql_free_result(res); mysql_close(&conn); return 0; } }
Last edited by whitemoss; Dec 4th, 2006 at 10:00 pm.
Your query has 13 columns in the resultset, but you are attempting to access 18 of them -- row[0] through row[17] is 18 columns, not 13. only row[0] through row[12] are valid.
Last edited by Ancient Dragon; Dec 4th, 2006 at 11:05 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
how to combine the rows?. actually i want to combine addr and addr2 to become for example address but separated with (,).
ley say addr = no.17
addr2 = loke yew road
and it should be = no.17,loke yew road
when it was printed it should be:
2006-12-02 13:53:24|0134216014|ZAHARI BIN MOHAMED|ZAHARI BIN MOHAMED|580513-03-5667|5598577||LOT 1075,KAMPUNG JERANGAU|17500 TANAH MERAH||17500|MAL
instead of
2006-12-02 13:53:24|0134216014|ZAHARI BIN MOHAMED|ZAHARI BIN MOHAMED|580513-03-5667|5598577||LOT 1075|KAMPUNG JERANGAU|17500 TANAH MERAH||17500|MAL
if the above query being printed
thanks
ley say addr = no.17
addr2 = loke yew road
and it should be = no.17,loke yew road
when it was printed it should be:
2006-12-02 13:53:24|0134216014|ZAHARI BIN MOHAMED|ZAHARI BIN MOHAMED|580513-03-5667|5598577||LOT 1075,KAMPUNG JERANGAU|17500 TANAH MERAH||17500|MAL
instead of
2006-12-02 13:53:24|0134216014|ZAHARI BIN MOHAMED|ZAHARI BIN MOHAMED|580513-03-5667|5598577||LOT 1075|KAMPUNG JERANGAU|17500 TANAH MERAH||17500|MAL
if the above query being printed
thanks
you can easily put the comma wherever you want it in the sprintf() format string. Just replace one of those pipe symbols | with a comma.
Last edited by Ancient Dragon; Dec 5th, 2006 at 8:45 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
you need to make your program a little more complex. Instead of one big spirntf() line you need to create a loop, for each row, search all rows from 0 to current row-1 to see if it is a duplicate, if not then use strcat() to copy its value into string1 array. Note that you will not use sprintf() at all.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
hi guys..
sorry for asking again regarding segmentation fault..my query consists of 12 columns in the resultset and I already called to access from row[0] until row[11]...when I try to run the program...I still got the segmentation fault...
here is my latest code:
and the result something like this:
it seems like, it fails to write into a file caused the segmentation fault...is it true? n how to solve this...many thanks...
sorry for asking again regarding segmentation fault..my query consists of 12 columns in the resultset and I already called to access from row[0] until row[11]...when I try to run the program...I still got the segmentation fault...
here is my latest code:
C Syntax (Toggle Plain Text)
sprintf(query1, "select a.MobileNumber, b.BillName, b.ICNew, b.Passport," "b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Postcode, b.CountryCode, a.CreateDate, a.Source from Register as a, Profile as b" " where a.MasterNumber = b.Number and a.Status=0"); //printf ("%s\n",query1); if (mysql_query(&conn, query1)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } res = mysql_use_result(&conn); /* output fields 0, 1 and 2 of each row */ while ((row = mysql_fetch_row(res)) != NULL) { if (i==11) { timebuffer(); i=1; bzero(string1, sizeof(string1)); } else { escapeSpecialChar(row[2]); strcpy(var1,row[9]); if ( strcmp(var1,"MAL" ) == 0 ) { strcpy (var1, "MALAYSIA"); strcpy(var2, var1); } DB2date(row[10]); sprintf(string1,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|%s|", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, row[11]); sprintf(mobileno,"%s", row[0]); printf ("%s\n",string1); //printf ("Test: %s\n",mobileno); writeLogFile(string1, mobileno); i++; } }
C Syntax (Toggle Plain Text)
void writeLogFile(char line2[1600], char* MobileNo) { char * mobile = "0192426699"; //char * mobile = MobileNo; mobile = MobileNo; FILE *fp; FILE *fp2; char len[255]; char filename[255]; int min; int hour; int day; int mon; int yr; time_t tvec = time(0); struct tm* dtime; //time(&tvec); dtime = localtime(&tvec); min = dtime->tm_min ; hour = dtime->tm_hour ; day = dtime->tm_mday ; mon = dtime->tm_mon + 1 ; yr = dtime->tm_year +1900; sprintf(filename,"twop_%04s""%02d""%02d""%02d""%02d"".dat",yr,mon,day,hour,min); // Filename Format twop_yyyyMMddhhmm.dat fp=fopen(filename, "a+"); if ( (fputs(line2, fp)) == EOF) { printf("error write..."); } if ( (fputs("\n", fp)) == EOF) { printf("error write1..."); } //printf ("Test2: %s\n",mobile); UpdateStatus(mobile, line2); bzero(line2, strlen(line2)); strcpy (line2, "1"); fclose(fp); }
C Syntax (Toggle Plain Text)
0135712114|MALIK BIN JANG|68100311355821||171, JLN KAMPUNG SERPAN LAUT,KAMPUNG SERPAN LAUT,|94600 ASAJAYA,|94600|MALAYSIA|05-12-2006|1| Segmentation fault
Last edited by whitemoss; Dec 10th, 2006 at 1:09 am.
![]() |
Similar Threads
- Segmentation fault in C++ (C++)
- Segmentation fault (Python)
- help on 'Segmentation Fault' (C)
- segmentation fault (C)
- Access Violation (Segmentation Fault) + atol (C++)
- unix/C++ segmentation fault (C++)
Other Threads in the C Forum
- Previous Thread: Deleting characters function ( with just one parameter)
- Next Thread: Link GPRS modem to I/O board?
| Thread Tools | Search this Thread |
adobe ansi api array asterisks binarysearch calculate centimeter changingto char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory fflush file fork forloop frequency givemetehcodez global grade graphics gtkgcurlcompiling hacking highest histogram homework i/o inches infiniteloop input interest iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match meter microsoft mysql number open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string suggestions systemcall threads turboc unix user variable voidmain() wab win32api windows.h windowsapi






