| | |
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 8:43 pm.
you should post variable declaractions too.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
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 9: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 10:05 pm.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
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 7:45 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
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.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
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 12: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?
Views: 4078 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C
api arguments array arrays binary binarysearch c++ char character code codes coke command conversion convert copy copyimagefile cpu database decimal directory dude dynamic ebook ebooks error exec factorial fgets file fork function functions getlasterror givemetehcodez grade graphics homework i/o input insert int integer lazy libcurl line linked linkedlist linux list lists locate logical_drives loop loops malloc matrix memory messagebox motherboard mysql no-effort opensource output path pause pointer pointers problem process program programming questions read recursion recursive recv reverse scanf single socketprograming socketprogramming spoonfeeding sql static string strings strtok structures student suggestions system systemcall turbo turbo-c turboc unix user variable windows






