i've changed my code and it's more simpler than before..I already can compile the code but then, when i'm trying to run it, i've got the "Segmentation Fault" error.
#include <mysql.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
MYSQL conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *host = "localhost";
char *user = "External";
char *password = "prepaidsms";
char *database = "External";
int tbuf;
char* buf;
char *line;
char MasterNo[50];
void writeLogFile(char line2[1600], char* MobileNo);
void UpdateStatus(char* MobileNo);
void timebuffer();
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.MobileNumber, a.MasterNumber, a.MasterAccNo, b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Addr5, b.Addr6,"
"b.Addr7, b.Addr8, b.Postcode, b.CountryCode, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport 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|%s|%s|%s|%s|%s|",
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]);
printf ("%s\n",string1);
writeLogFile(string1, mobileno);
i++;
}
}
/* Release memory used to store results and close connection */
mysql_free_result(res);
mysql_close(&conn);
return 0;
}
void writeLogFile(char line2[1600], char* MobileNo)
{
char Temp[55];
char Year[5];
char filename[255];
char *mon;
char *day;
char *yr;
time_t tvec;
struct tm* dtime;
time(&tvec);
dtime = localtime(&tvec);
*mon = dtime->tm_mon + 1 ;
*day = dtime->tm_mday ;
*yr = dtime->tm_year ;
sprintf(filename,"DATA-%02d-02d-04d.txt",
mon,
day,
yr);
// Filename Format YYYY-MM-DD
printf ("%s\n",filename);
FILE *fp;
fp=fopen(filename, "a+");
if ( (fputs(line2, fp)) == EOF)
{
printf("error write...");
}
if ( (fputs("\n", fp)) == EOF)
{
printf("error write1...");
}
fclose(fp);
}
i've changed sumthing to the coding. i just changed sprintf(filename,"DATA-%02d-02d-04
it seems like, there is sumthing wrong with filename..and no file actually was created..