943,967 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5378
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 4th, 2006
0

Segmentation Fault error

Expand Post »
Hi All,

Need our expertise out there regarding the above mention error and below are the codes:

  1. /* send SQL query */
  2.  
  3. bzero(query1, sizeof(query1));
  4. sprintf(query1, "select a.CreateDate, a.MobileNumber, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport,"
  5. "b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Postcode, b.CountryCode from Register as a, Profile as b"
  6. " where a.MasterNumber = b.Number and a.Status=0");
  7.  
  8. printf ("%s\n",query1);
  9.  
  10. if (mysql_query(&conn, query1))
  11. {
  12. fprintf(stderr, "%s\n", mysql_error(&conn));
  13. exit(0);
  14. }
  15.  
  16.  
  17. res = mysql_use_result(&conn);
  18.  
  19.  
  20. /* output fields 0, 1 and 2 of each row */
  21. while ((row = mysql_fetch_row(res)) != NULL)
  22. {
  23. if (i==11)
  24. {
  25. timebuffer();
  26. i=1;
  27. bzero(string1, sizeof(string1));
  28. }
  29. else
  30. {
  31. sprintf(string1,"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",
  32. row[0], row[1], row[14], row[15], row[16], row[17], row[18], row[4],
  33. row[5], row[6], row[7], row[12], row[13]);
  34. sprintf(mobileno,"%s", row[1]);
  35. printf ("%s\n",string1);
  36.  
  37. printf ("Test: %s\n",mobileno);
  38. writeLogFile(string1, mobileno);
  39. i++;
  40. }
  41. }
When I run the program, it gives me "Segmentation Fault" and I think it's mayb becos of combining 2 tables.

Many thanks..
Last edited by whitemoss; Dec 4th, 2006 at 9:43 pm.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
whitemoss is offline Offline
32 posts
since Jul 2006
Dec 4th, 2006
0

Re: Segmentation Fault error

you should post variable declaractions too.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 4th, 2006
0

Re: Segmentation Fault error

sorry..aku post again my codes:

  1. int main(int argc, char *argv[])
  2. {
  3.  
  4. char string1[1600], string2[1600], query[1600], query1[1600], mobileno[50];
  5. int i, j, len;
  6. FILE *fp2;
  7.  
  8.  
  9. mysql_init(&conn);
  10.  
  11. /* Connect to database */
  12. if (!mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0)) {
  13. fprintf(stderr, "%s\n", mysql_error(&conn));
  14. exit(0);
  15. }
  16.  
  17. /* send SQL query */
  18.  
  19. bzero(query1, sizeof(query1));
  20. sprintf(query1, "select a.CreateDate, a.MobileNumber, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport,"
  21. "b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Postcode, b.CountryCode from Register as a, Profile as b"
  22. " where a.MasterNumber = b.Number and a.Status=0");
  23.  
  24. printf ("%s\n",query1);
  25.  
  26. if (mysql_query(&conn, query1))
  27. {
  28. fprintf(stderr, "%s\n", mysql_error(&conn));
  29. exit(0);
  30. }
  31.  
  32.  
  33. res = mysql_use_result(&conn);
  34.  
  35.  
  36. /* output fields 0, 1 and 2 of each row */
  37. while ((row = mysql_fetch_row(res)) != NULL)
  38. {
  39. if (i==11)
  40. {
  41. timebuffer();
  42. i=1;
  43. bzero(string1, sizeof(string1));
  44. }
  45. else
  46. {
  47. sprintf(string1,"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",
  48. row[0], row[1], row[14], row[15], row[16], row[17], row[18], row[4],
  49. row[5], row[6], row[7], row[12], row[13]);
  50. sprintf(mobileno,"%s", row[1]);
  51. printf ("%s\n",string1);
  52.  
  53. printf ("Test: %s\n",mobileno);
  54. writeLogFile(string1, mobileno);
  55. i++;
  56. }
  57.  
  58. /* Release memory used to store results and close connection */
  59. mysql_free_result(res);
  60. mysql_close(&conn);
  61.  
  62. return 0;
  63. }
  64.  
  65. }
Last edited by whitemoss; Dec 4th, 2006 at 10:00 pm.
Reputation Points: 16
Solved Threads: 0
Light Poster
whitemoss is offline Offline
32 posts
since Jul 2006
Dec 4th, 2006
0

Re: Segmentation Fault error

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 4th, 2006
0

Re: Segmentation Fault error

ooohhhh..thanks..i thought..the declared row depends on the row inside the table of database..
Reputation Points: 16
Solved Threads: 0
Light Poster
whitemoss is offline Offline
32 posts
since Jul 2006
Dec 4th, 2006
0

Re: Segmentation Fault error

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
Reputation Points: 16
Solved Threads: 0
Light Poster
whitemoss is offline Offline
32 posts
since Jul 2006
Dec 5th, 2006
0

Re: Segmentation Fault error

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 6th, 2006
0

Re: Segmentation Fault error

ok..that's 1 idea...how about if there is 2 rows that have same value but i should display only 1..meaning that, i should check if 2 column have same value..i should display only 1..


Thanks for ur help...
Reputation Points: 16
Solved Threads: 0
Light Poster
whitemoss is offline Offline
32 posts
since Jul 2006
Dec 6th, 2006
0

Re: Segmentation Fault error

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 10th, 2006
0

Re: Segmentation Fault error

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:
  1. sprintf(query1, "select a.MobileNumber, b.BillName, b.ICNew, b.Passport,"
  2. "b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Postcode, b.CountryCode, a.CreateDate, a.Source from Register as a, Profile as b"
  3. " where a.MasterNumber = b.Number and a.Status=0");
  4.  
  5. //printf ("%s\n",query1);
  6.  
  7. if (mysql_query(&conn, query1))
  8. {
  9. fprintf(stderr, "%s\n", mysql_error(&conn));
  10. exit(0);
  11. }
  12.  
  13.  
  14. res = mysql_use_result(&conn);
  15.  
  16.  
  17. /* output fields 0, 1 and 2 of each row */
  18. while ((row = mysql_fetch_row(res)) != NULL)
  19. {
  20. if (i==11)
  21. {
  22. timebuffer();
  23. i=1;
  24. bzero(string1, sizeof(string1));
  25. }
  26. else
  27. {
  28. escapeSpecialChar(row[2]);
  29.  
  30. strcpy(var1,row[9]);
  31. if ( strcmp(var1,"MAL" ) == 0 )
  32. {
  33. strcpy (var1, "MALAYSIA");
  34. strcpy(var2, var1);
  35. }
  36.  
  37. DB2date(row[10]);
  38.  
  39. sprintf(string1,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|%s|",
  40. row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, row[11]);
  41. sprintf(mobileno,"%s", row[0]);
  42. printf ("%s\n",string1);
  43.  
  44. //printf ("Test: %s\n",mobileno);
  45. writeLogFile(string1, mobileno);
  46. i++;
  47. }
  48. }
  1. void writeLogFile(char line2[1600], char* MobileNo)
  2. {
  3. char * mobile = "0192426699";
  4. //char * mobile = MobileNo;
  5. mobile = MobileNo;
  6.  
  7. FILE *fp;
  8. FILE *fp2;
  9. char len[255];
  10. char filename[255];
  11. int min;
  12. int hour;
  13. int day;
  14. int mon;
  15. int yr;
  16. time_t tvec = time(0);
  17. struct tm* dtime;
  18.  
  19. //time(&tvec);
  20. dtime = localtime(&tvec);
  21.  
  22. min = dtime->tm_min ;
  23. hour = dtime->tm_hour ;
  24. day = dtime->tm_mday ;
  25. mon = dtime->tm_mon + 1 ;
  26. yr = dtime->tm_year +1900;
  27.  
  28. sprintf(filename,"twop_%04s""%02d""%02d""%02d""%02d"".dat",yr,mon,day,hour,min);
  29.  
  30. // Filename Format twop_yyyyMMddhhmm.dat
  31.  
  32.  
  33. fp=fopen(filename, "a+");
  34.  
  35. if ( (fputs(line2, fp)) == EOF)
  36. {
  37. printf("error write...");
  38. }
  39.  
  40. if ( (fputs("\n", fp)) == EOF)
  41. {
  42. printf("error write1...");
  43. }
  44.  
  45. //printf ("Test2: %s\n",mobile);
  46. UpdateStatus(mobile, line2);
  47. bzero(line2, strlen(line2));
  48. strcpy (line2, "1");
  49.  
  50. fclose(fp);
  51. }
and the result something like this:
  1. 0135712114|MALIK BIN JANG|68100311355821||171, JLN KAMPUNG SERPAN LAUT,KAMPUNG SERPAN LAUT,|94600 ASAJAYA,|94600|MALAYSIA|05-12-2006|1|
  2. Segmentation fault
it seems like, it fails to write into a file caused the segmentation fault...is it true? n how to solve this...many thanks...
Last edited by whitemoss; Dec 10th, 2006 at 1:09 am.
Reputation Points: 16
Solved Threads: 0
Light Poster
whitemoss is offline Offline
32 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Deleting characters function ( with just one parameter)
Next Thread in C Forum Timeline: guess who! Run-time error!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC