IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

mysql_exec_sql, segault dans un loop


Sujet :

C

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut mysql_exec_sql, segault dans un loop
    Salut a tous!

    J'ai cette fonction qui est appeler dans un loop dans le main.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
     
    int  import_image(char *image_filename, char *make1, char *model1, char *year1)
    {
    	char *make;
    	char *model;
    	char *year;
    	char *file = "car_id";
    	char thumb[100];
    	char id[50];
    	char filex[15];
    	int d;
     
    	MYSQL mysql;
    	MYSQL_RES *result;
    	MYSQL_ROW row;
     
    	unsigned int num_fields;
    	unsigned int i;
    	char query_def[1000];
     
    	make = str_replace("\"", "", make1);
    	model = str_replace("\"", "", model1);
    	year = str_replace("\"", "", year1);
     
    	//start mysql sequence to get the last id of the car inserted...
    	if(mysql_init(&mysql)==NULL)
    	{
    		printf("\nFailed to initate MySQL connection");
    		exit(1);
    	}
    	sprintf(query_def,"SELECT t1.car_id FROM tbl_cars AS t1 ORDER BY t1.car_id DESC LIMIT 1");
     
    	//test local
    	if (!mysql_real_connect(&mysql,"localhost","root","",NULL,0,NULL,0)) 
    	{
    	    printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(&mysql));
    	    exit(1);
    	}
     
    	if(mysql_select_db(&mysql,"test_import")==0)
     
    	{
    		if(mysql_exec_sql(&mysql,query_def)==0) <--- ligne qui segfault
    		{
    	/*		MYSQL_RES *result;
    			result = mysql_use_result(&mysql);
     
    			if (result)  // there are rows
    			{
    				// retrieve rows, then call mysql_free_result(result)
    				MYSQL_ROW row;
    				if((row = mysql_fetch_row(result)))
    				{
    					unsigned long *lengths = mysql_fetch_lengths(result);
    					if(lengths[0] > 0)
     
    					{
    					  printf("%s", row[0]);
    					  int i;
    					  char ran[11];
    					  i = rand();
    					  sprintf(ran, "%d", i);
    					  strcpy(id, row[0]);
    					  d = strlen(id);
    					  strcat(filex, file);
    					  strcat(filex, id);
    					  strcat(filex, "_");
    					  strcat(filex, ran);
    					  strcpy(thumb, filex);
    					  strcat(filex, "_large.jpg");
    					  strcat(thumb, ".jpg");
    					  //all good? check the directory...
    					  check_directory(image_filename, filex, thumb, make, model, year);
    				   	 // exit(0);
    					}
    				}
    				mysql_free_result(result);
    			}*/
    		}
    		free(query_def);
    	//	mysql_close(&mysql);
     
    	}
     
    	    //printf( "Database Selected\n");
    	//else
    	  //  printf( "Failed to connect to Database: Error: %s\n", mysql_error(&mysql));
     
    	//mysql_close(&mysql);
     
    }
    Je me demande si c'est parce que je ne free pas query_def, mais meme avec un free, pareil...

  2. #2
    Invité(e)
    Invité(e)
    Par défaut
    Bonjour

    Ici, tu défini un tableau statique :
    Ici, la libération d'un tableau dynamique :
    Il va falloir choisir, soit garder query_def statique, dans ce cas, pas besoin de le libérer.

    soit le gérer dynamiquement avec malloc / free.

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    yep ok, mais que je le free ou pas, j'ai tjs mon segfault. Est ce que je dois reintialiser la chaine a "" ?

  4. #4
    Invité(e)
    Invité(e)
    Par défaut
    Citation Envoyé par Pitou5464 Voir le message
    mais que je le free ou pas, j'ai tjs mon segfault.
    En effet, j'avais pas vu à la première lecture où était située l'erreur.

    Citation Envoyé par Pitou5464 Voir le message
    Est ce que je dois reintialiser la chaine a "" ?
    A priori non, puisqu'elle est correctement initialisée ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    sprintf(query_def,"SELECT t1.car_id FROM tbl_cars AS t1 ORDER BY t1.car_id DESC LIMIT 1");
    peut être faut il commencer par plus simple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(mysql_exec_sql(&mysql, "SELECT * FROM tbl_cars")==0)
    puis,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    sprintf(query_def, "SELECT * FROM tbl_cars");
    /* ... */
    if(mysql_exec_sql(&mysql,query_def)==0)
    enfin
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    sprintf(query_def, "SELECT t1.car_id FROM tbl_cars AS t1 ORDER BY t1.car_id DESC LIMIT 1");
    /* ... */
    if(mysql_exec_sql(&mysql,query_def)==0)
    pour voir si le problème vient de la requête ou du code...

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
     
    	if(mysql_select_db(&mysql,"test_import")==0)
     
    	{
     
    		if(mysql_exec_sql(&mysql,query_def)==0)
    		{
    			MYSQL_RES *result;
    			result = mysql_use_result(&mysql);
    /*
    			if (result)  // there are rows
    			{
    				// retrieve rows, then call mysql_free_result(result)
    				MYSQL_ROW row;
    				if((row = mysql_fetch_row(result)))
    				{
    					unsigned long *lengths = mysql_fetch_lengths(result);
    					if(lengths[0] > 0)
     
    					{
    					 // printf("%s", row[0]);
     
    					  //build the new image file name like car_id213123_45645646_large.jpg and car_id213123_45645646.jpg
    					  int i;
    					  char ran[11];
    					  i = rand();
    					  sprintf(ran, "%d", i);
    					  strcpy(id, row[0]);
    					  d = strlen(id);
    					  strcat(filex, file);
    					  strcat(filex, id);
    					  strcat(filex, "_");
    					  strcat(filex, ran);
    					  strcpy(thumb, filex);
    					  strcat(filex, "_large.jpg");
    					  strcat(thumb, ".jpg");
    					  //all good? check the directory...
    					 // check_directory(image_filename, filex, thumb, make, model, year);
    					  //printf("%s\n", filex);
    					  //printf("%s\n", thumb);
    					  //printf("%s", ran);
    				   	  //exit(0);
    					  free(*lengths);
    					}
    				//exit(0);
    				}
    				mysql_free_result(result);
    			}*/
    	//free(*query_def);
    	//sprintf(query_def, "%s", "");
    		}
    	//	free(query_def);
    		mysql_close(&mysql);
     
    	}
    ./parse
    *** glibc detected *** ./parse: malloc(): memory corruption: 0x0806e3b8 ***

    est ce qu'il faut malloc les variables mysql?

  6. #6
    Invité(e)
    Invité(e)
    Par défaut
    Hum...

    Quel est le système utilisé ?

    libmysql et le système ne reposent ils pas sur deux versions de glibc différentes ?

  7. #7
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    613
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 613
    Par défaut
    es tu sur que ça plante des cette fonction que tu nous montre ?
    On peut pas avoir un code complet compilable pour vérifier ?

  8. #8
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    ok voici le code complet, c'est un premier jet et ca fait longtemps que j'ai pas coder en C...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
     
    //how to compile gcc -lmysqlclient -lgd -o parse parse2.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "/usr/include/mysql/mysql.h"
    #include "/usr/local/include/gd.h"
    #include <syslog.h>/*syslog*/
     
    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif
     
    #ifndef WIN32
      #include <unistd.h>
    #endif
     
    #define LBUFSIZ 1000
     
    char *resizeImage_edit(large_image_name, image){
    	/*
    	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
     
    	$source = imagecreatefromjpeg($image);
     
    	imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width_orig,$height_orig);
     
    	imagejpeg($newImage,$large_image_name,85);
     
    	chmod($large_image_name, 0777);
     
    	return $large_image_name;*/
     
    	int newImageWidth = 312;
     
    	int newImageHeight = 225;
    	int width;
    	int height;
    	gdImagePtr im;
    	gdImagePtr im2;
    	gdImagePtr im3;
    	FILE *in;
    	in = fopen(image, "r");
    	FILE *out;
    	out = fopen(large_image_name, "w");
     
    	//get original width and heights...
    	im = gdImageCreateFromJpeg(in);
     
    	width = gdImageSX(im);
    	height = gdImageSY(im);
     
    	//create new image
    	im2 = gdImageCreateTrueColor(newImageWidth, newImageHeight); //im2 == newImage
     
    	//imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width_orig,$height_orig);
    	gdImageCopyResampled(im2,im,0,0,0,0, newImageWidth,newImageHeight,width,height);
     
    	gdImageJpeg(im2,out,100);
     
    	fclose(in);
    /*	fclose(out);
    	gdImageDestroy(im);
    	gdImageDestroy(out);
    */
     
    	//chmod(large_image_name, 0777);*/
     
    	return large_image_name;
     
    }
     
    int check_dir(char *path1, char *path2, char *path3, char *path4) {
     
    /* Trim and clean white space...todo
    		$tab['make'] = trim($tab['make']);
    		$tab['model'] = trim($tab['model']);
     
    		$make = str_replace(' ', '_', $tab['make']);
     
    		$model = str_replace(' ', '_', $tab['model']);
     
    		$path_to_car = '../images/photosAutopress/Anza_Motor_Company/' . $make . '/' .$tab['year'] . '/' . $model;
     
    		$path_to_car2 = '../images/photosAutopress/Anza_Motor_Company/' . $make . '/' .$tab['year'];
     
    		$path_to_car3 = '../images/photosAutopress/Anza_Motor_Company/' . $make;
     
    		$path_to_car4 = '../images/photosAutopress/Anza_Motor_Company';*/
     
     
    		//for windobe
    		//_mkdir( path.c_str() ); 
     
     
    		if (fopen(path1,"r") == NULL)
     
    		{
     
    			if (fopen(path2, "r") == NULL)
     
    				{
     
    				if (fopen(path3, "r") == NULL)
     
    					{
     
    					if (fopen(path4, "r") == NULL)
     
    						mkdir(path4, 0777);
     
    					else 
     
    						mkdir(path3, 0777);
     
    					}	
     
    				else
     
    					mkdir(path2, 0777);
     
    				}
     
    			else 
    				{
     
    				mkdir(path1, 0777);
    				}
     
    		}
     
    		if (fopen(path1, "r") != NULL) {
     
    			return 1;
     
    		}
     
    		else
     
    		{
     
    			return 0;
     
    		}
     
     
     
    }
     
    char *strip(char *src,char *dst)
    {
        int src_len=0;
        int i=0;
     
        src_len=strlen(src);//Recupere la longueur de la chaine de depart
     
        dst=src;
     
        while((i<src_len)&&(src[i]==' '))
        {
             *dst++;//Place le pointeur dst au debut de la chaine sans espace
             i++;
        }
     
        i=src_len-2;//-2 pour eviter le caractere \0
     
        while((i>=0)&&(src[i]==' '))
        {
             src[i]='\0';
             i--;
        }
       return (dst);
    }
     
    int mysql_exec_sql(MYSQL *mysql,const char *create_definition)
    {
       return mysql_real_query(mysql,create_definition,strlen(create_definition));
    }
     
    char *str_replace(char * t1, char * t2, char * t6){
        	char*t4;
        	char*t5=malloc(0);
     
     
            	while(strstr(t6,t1)){
            		t4=strstr(t6,t1);
            		strncpy(t5+strlen(t5),t6,t4-t6);
            		strcat(t5,t2);
            		t4+=strlen(t1);
            		t6=t4;
            	}
            	return strcat(t5,t4);
    }
     
    int  treat_image(char *path, char *image_filename, char *filename, char *thumb, char *make1, char *model1, char *year1)
    {
    	char path_final[500];
     
    	sprintf(path_final, "%s/%s", path, filename);
     
    	if (fopen(image_filename, "r") != NULL)
    		{
    		//file3 == path_final
    		resizeImage_edit(path_final, image_filename);
    	//	resizeThumbnailImage_edit($thumb_image_location, $file3,$w,$h,$x1,$y1,$scale);
    		}
     
    }
     
    int  check_directory(char *image_filename, char *filename, char *thumb, char *make1, char *model1, char *year1)
    {
    	char *make;
    	char *model;
    	char *year;
    	char dealer_image_path[500];
    	char str[500];
    	char path1[500];
    	char path2[500];
    	char path3[500];
    	char path4[500];
    	char *path1_1 = path1;
    	char *path2_1 = path2;
    	char path3_1[500];
    	char path4_1[500];
    	int  z;
     
    	sprintf(str,"%s%s/%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1, model1);
    	sprintf(path1,"%s%s/%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1, model1);
    	sprintf(path2,"%s%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1);
    	sprintf(path3,"%s%s", "../images/photosAutopress/Anza_Motor_Company/", make1);
    	sprintf(path4,"%s", "../images/photosAutopress/Anza_Motor_Company/");
     
    	while (z != 1)
    	{
    		z  =check_dir(path1, path2, path3, path4);
    		if (z == 1)
    			treat_image(path1, image_filename, filename, thumb, make1, model1, year1);
    	}
     
    	//printf("%d", z);
    	//exit(0);
    }
     
    int  import_image(char *image_filename, char *make1, char *model1, char *year1)
    {
     
    	char *make;
    	char *model;
    	char *year;
    	char *file = "car_id";
    	char thumb[100];
    	char id[50];
    	char filex[15];
    	int d;
     
    	MYSQL mysql;
    	MYSQL_RES *result;
    	MYSQL_ROW row;
     
    	unsigned int num_fields;
    	unsigned int i;
    	//char query_def[1000];
     
    	make = str_replace("\"", "", make1);
    	model = str_replace("\"", "", model1);
    	year = str_replace("\"", "", year1);
     
    /*	printf("%s\n", image_filename);
    printf("%s\n", make);
    printf("%s\n", model);
    printf("%s\n", year);*/
     
     
    	//start mysql sequence to get the last id of the car inserted...
    	if(mysql_init(&mysql)==NULL)
    	{
    		printf("\nFailed to initate MySQL connection");
    		exit(1);
    	}
     
    	if(!mysql_real_connect (&mysql,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql));
    			 mysql_close(&mysql);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql,"SELECT t1.car_id, t1.car_make, t1.car_model, t1.car_year, t2.car_dealer_id FROM tbl_cars AS t1 LEFT JOIN tbl_cars_details AS t2 ON t1.car_id = t2.car_id ORDER BY t1.car_id DESC LIMIT 1"))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql));
    				 mysql_close(&mysql);
    				 exit(0);
    		}
    		else
    		{
    			MYSQL_RES *result;
    			result = mysql_use_result(&mysql);
     
    			if (result)  // there are rows
    			{
     
    				// retrieve rows, then call mysql_free_result(result)
    				MYSQL_ROW row;
    				if((row = mysql_fetch_row(result)))
    				{
    					unsigned long *lengths = mysql_fetch_lengths(result);
    					if(lengths[0] > 0)
     
    					{
    					  printf("%s\n", row[0]);
     
    					  //build the new image file name like car_id213123_45645646_large.jpg and car_id213123_45645646.jpg
    					  int i;
    					  char ran[11];
    					  i = rand();
    					  sprintf(ran, "%d", i);
    					  strcpy(id, row[0]);
    					  d = strlen(id);
    					  strcat(filex, file);
    					  strcat(filex, id);
    					  strcat(filex, "_");
    					  strcat(filex, ran);
    					  strcpy(thumb, filex);
    					  strcat(filex, "_large.jpg");
    					  strcat(thumb, ".jpg");
    					  //all good? check the directory...
    					 // check_directory(image_filename, filex, thumb, make, model, year);
     
    					  return (1);
     
    					}
     
    				}
    				//mysql_free_row(row);
    				mysql_free_result(result);
    			}
    			else
    			{			
    				mysql_free_result(result);
    				return (0);
    			}
    		}
     
    		mysql_close(&mysql);
    		//free(*mysql);
    		return(1);
     
    }
     
    char *loop_check_images(char *id, int p)
    {
    	int x;
    	int j;
    	int k;
    	int length_id;
    	int length_id2;
    	char *str_test;
    	char *str_test1;
    	char *str_test2;
    	char *str_test3;
    	char *blah;
    	char buf[10];
      	int  i, z;
    	char *test;
    	char *test1;
      	FILE *fp1;
     
    	length_id = strlen(id);
    	length_id2 = length_id + 6;
     
    	//length_id++;
     
    	//for (x=1;x<=10;x++)
    	//{
     
    	str_test = (char *)malloc(sizeof(char [length_id+1]));
    	blah = str_replace("\"", "", id);
    	test = "./";
    	str_test = strcat(blah, "-");
    	z = strlen(str_test);
    	test1 = (char *)malloc(sizeof(char [z+3]));
    	test1 = strcat(test1, "./");
    	test1 = strcat(test1, str_test);
    	z = strlen(test1);
     
    		str_test1 = (char *)malloc(sizeof(char [z+5]));
    		sprintf(buf, "%d", p);
    		//printf("%s\n", buf);
    		str_test1 = strcat(test1, buf);
    		str_test2 = strcat(str_test1, ".JPG");
    		//Use fopen to see if the file exist, if NULL doesnt exist
      		fp1=fopen(str_test2,"r");
    		if (fp1 == NULL)
    		{
    			str_test2 = "";
    			return (str_test2);
    			//treat if image doesnt exist
    		  	//printf("%s %s\n", str_test2, "image doesn't exist");
    		}
    		else
    			return (str_test2);	
     
    	//}
    	//exit(0);
    }
     
    int	insert_details_in_db(char *cc, char *transmission, char *color, char *description, char *description1, char *id)
    {
    	char	query2[500];
    	MYSQL mysql2;
    	mysql_init(&mysql2);
    	char *dealer_id = "78";
    	char *used = "1";
    	char *description2;
    	char *description3;
    	char *str1;
    	char *str2;
     
    	description2 = strcat(description, description1);
     
    	sprintf(query2,"INSERT INTO tbl_cars_details (`car_dealer_id`, `car_description`, `car_used`, `car_dealer_stock_number`) VALUES (%s, '%s', '%s', %s)", dealer_id, description2, used, id);
     
    	if(!mysql_real_connect (&mysql2,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql2));
    			 mysql_close(&mysql2);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql2,query2))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql2));
    				 mysql_close(&mysql2);
    				 exit(0);
    		}
    		else
    			return 1;
    	mysql_close(&mysql2);
     
    }
     
     
    int	insert_file_in_db(char *make, char *model, char *year, char *price)
    {
    	char	query[500];
    	MYSQL mysql;
    	mysql_init(&mysql);
    	char *category = "5";
     
    	sprintf(query,"INSERT INTO tbl_cars (`car_make`, `car_model`, `car_year`, `car_price`, `car_category`) VALUES (%s, %s, %s, %s, %s)", make, model, year, price, category);
     
    	if(!mysql_real_connect (&mysql,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql));
    			 mysql_close(&mysql);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql,query))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql));
    				 mysql_close(&mysql);
    				 exit(0);
    		}
    		else
    			return 1;
     
    	mysql_close(&mysql);
     
    }
     
    int main()
    {
      char input_line[LBUFSIZ];
      char *str_image_filename;
     
      struct {
        char id[50];
        char make[50];
        char model[50];
        char model4[50];
        char color[50];
        char year[4];
        char empty[5];
        char transmission[15];
        char price[8];
        char description[400];
        char description1[400];
        char cc[6];
     
      }car_details[2500];
      FILE *fp;
      char buf[500];
      char *model5;
      char *id;
      char *make;
      char *model;
      char *empty;
      char *year;
      char *color;
      char *transmission;
      char *description;
      char *description1;
      char *cc;
      char *price;
      char *make1;
      char *model1;
      char *model2;
      char *year1;
      char *price1;
     
      int x;
      int y;
      int i = 0, j;
      int d = 0;
     
      fp=fopen("Anzabike2.txt","r");
     
      while (fgets(input_line, sizeof input_line, fp))
      { 
     
        id = strtok(input_line, ",");
        make = strtok(NULL, ",");
        model = strtok(NULL, ",");
        model2 = strtok(NULL, ",");
        year = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        transmission = strtok(NULL, ",");
        color = strtok(NULL, ",");
        price = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        description = strtok(NULL, ",");
        description1 = strtok(NULL, "\"");
        empty = strtok(NULL, ",");
        cc = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        strcpy(car_details[i].id, id);
        strcpy(car_details[i].make, make);
        strcpy(car_details[i].model, model);
        strcpy(car_details[i].year, year);
        strcpy(car_details[i].transmission, transmission);
        strcpy(car_details[i].color, color);
        strcpy(car_details[i].price, price);
        strcpy(car_details[i].description, description);
        strcpy(car_details[i].description1, description1);
        strcpy(car_details[i].cc, cc);
        strcpy(car_details[i].model4, model2);
        ++i;
      }
     
      d = strlen(car_details);
      int p;
      int m;
      for (j=0; j<d; j++)
      {
        model5 = strdup(car_details[j].model);
        if (strcmp(model5, "\" \"") == 0)
    	{
    	    strcat(model5, car_details[j].model4);
    	   model5 = str_replace("\" \"", "", model5);
    	}
    	//model5 = str_replace(" ", "", model5);
        		//printf("%s\n", model5);
        x = insert_file_in_db(car_details[j].make, car_details[j].model, car_details[j].year, car_details[j].price);
        if (x = 1)
    	    y = insert_details_in_db(car_details[j].cc, car_details[j].transmission, car_details[j].color, car_details[j].description, car_details[j].description1, car_details[j].id);
    	if (y = 1)
    	{
    		// import the images
    		for (p=0;p<=3;p++)
    		{
    			str_image_filename = loop_check_images(car_details[j].id, p);
    			if (strcmp(str_image_filename, "") != 0)
    			  {
    				m = import_image(str_image_filename, car_details[j].make, model5, car_details[j].year);
    				if (m == 1)
    					printf("%s %s", str_image_filename, "image importe");
    			  }
    		}
     
    	}
     
     }
     
      return 0;
    }
    system ubuntu hardy
    Fichiers attachés Fichiers attachés

  9. #9
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    613
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 613
    Par défaut
    Oula, il y a beaucoup de choses a corriger :
    On commence par les avertissements de compilations les plus importantes :
    parse.c: In function ‘resizeImage_edit’:
    parse.c:45: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast
    parse.c:47: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast
    parse.c:71: warning: return makes pointer from integer without a cast
    parse.c: In function ‘main’:
    parse.c:565: warning: passing argument 1 of ‘strlen’ from incompatible pointer type
    Pour corriger ca :
    La fonction resizeImage_edit se définit comme ceci :
    char *resizeImage_edit(char * large_image_name, char * image)

    strlen(car_details); n'est pas valide car car_details n'est pas une chaine de caractere.
    Tu peux a la place défnir cette taille dans un define en début de code :
    #define TAILLE_CAR_DETAIL 2500
    puis remplacer car_details[2500]; par car_details[TAILLE_CAR_DETAIL];
    et enfin d = TAILLE_CAR_DETAIL;

    Enuite recompile ce code en ajoutant -Wall apres gcc et corrige tous les autres avertissements.

    J'ai tout de meme essayé de lancer ton code avec ces premieres corrections et je plante ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
      fp=fopen("Anzabike2.txt","r");
     
      while (fgets(input_line, sizeof input_line, fp))
    Normal car ce fichier n'existe pas chez moi et tu ne vérifie pas si l'ouverture s'est bien passée (erreur !! )
    donc il faut faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     fp=fopen("Anzabike2.txt","r");
     if (fp == NULL)
    {
     // gerer cette erreur d'ouverture de fichier
    }

    Voilà je te laisse continuer, demande si tu as d'autrs questions

    Autre petite chose :
    tu peux remplacer
    #include "/usr/include/mysql/mysql.h"
    #include "/usr/local/include/gd.h"

    par

    #include <mysql/mysql.h>
    #include <gd.h>

    C'est plus propre

  10. #10
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    merci pour ton aide,

    Je vais appliquer ca illico.
    En ce qui concerne le fichier. J'ai editer mon precedent post.

  11. #11
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    613
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 613
    Par défaut
    Oui j'avais pas vu le fichier. Par contre tu dois quand meme vérifier ce fopen.
    Je ne plante plus si je met le fichier.

  12. #12
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    c'est clair. J'avais identifie que le segfault est dans la fonction import image. J'appelle cette fonction dans un loop a partir du main, il fait la premiere, mais apres il segfault.
    Et j'ai l'impression que c'est quand il reexecute la meme query.
    Avec tout ce que tu m'as montrer a verifier et corriger, ca peut venir d'autre part, mais je trouve pas.

  13. #13
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    613
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 613
    Par défaut
    reposte ton code corrigé

  14. #14
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    J'ai corrige quelques trucs...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
     
    //how to compile gcc -Wall -lmysqlclient -lgd -o parse parse2.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <mysql/mysql.h>
    #include <gd.h>
    #include <syslog.h>/*syslog*/
    #include <sys/stat.h>
    #include <sys/types.h>
     
    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif
     
    #ifndef WIN32
      #include <unistd.h>
    #endif
     
    #define LBUFSIZ 1000
     
    char *resizeImage_edit(char *large_image_name, char *image){
    	/*
    	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
     
    	$source = imagecreatefromjpeg($image);
     
    	imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width_orig,$height_orig);
     
    	imagejpeg($newImage,$large_image_name,85);
     
    	chmod($large_image_name, 0777);
     
    	return $large_image_name;*/
     
    	int newImageWidth = 312;
     
    	int newImageHeight = 225;
    	int width;
    	int height;
    	gdImagePtr im;
    	gdImagePtr im2;
    //	gdImagePtr im3;
    	FILE *in;
    	in = fopen(image, "r");
    	FILE *out;
    	out = fopen(large_image_name, "w");
     
    	//get original width and heights...
    	im = gdImageCreateFromJpeg(in);
     
    	width = gdImageSX(im);
    	height = gdImageSY(im);
     
    	//create new image
    	im2 = gdImageCreateTrueColor(newImageWidth, newImageHeight); //im2 == newImage
     
    	//imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width_orig,$height_orig);
    	gdImageCopyResampled(im2,im,0,0,0,0, newImageWidth,newImageHeight,width,height);
     
    	gdImageJpeg(im2,out,100);
     
    	fclose(in);
    /*	fclose(out);
    	gdImageDestroy(im);
    	gdImageDestroy(out);
    */
     
    	//chmod(large_image_name, 0777);*/
     
    	return large_image_name;
     
    }
     
    int check_dir(char *path1, char *path2, char *path3, char *path4) {
     
    /* Trim and clean white space...todo
    		$tab['make'] = trim($tab['make']);
    		$tab['model'] = trim($tab['model']);
     
    		$make = str_replace(' ', '_', $tab['make']);
     
    		$model = str_replace(' ', '_', $tab['model']);
     
    		$path_to_car = '../images/photosAutopress/Anza_Motor_Company/' . $make . '/' .$tab['year'] . '/' . $model;
     
    		$path_to_car2 = '../images/photosAutopress/Anza_Motor_Company/' . $make . '/' .$tab['year'];
     
    		$path_to_car3 = '../images/photosAutopress/Anza_Motor_Company/' . $make;
     
    		$path_to_car4 = '../images/photosAutopress/Anza_Motor_Company';*/
     
     
    		//for windobe
    		//_mkdir( path.c_str() ); 
     
     
    		if (fopen(path1,"r") == NULL)
     
    		{
     
    			if (fopen(path2, "r") == NULL)
     
    				{
     
    				if (fopen(path3, "r") == NULL)
     
    					{
     
    					if (fopen(path4, "r") == NULL)
     
    						mkdir(path4, 0777);
     
    					else 
     
    						mkdir(path3, 0777);
     
    					}	
     
    				else
     
    					mkdir(path2, 0777);
     
    				}
     
    			else 
    				{
     
    				mkdir(path1, 0777);
    				}
     
    		}
     
    		if (fopen(path1, "r") != NULL) {
     
    			return 1;
     
    		}
     
    		else
     
    		{
     
    			return 0;
     
    		}
     
     
     
    }
     
    char *strip(char *src,char *dst)
    {
        int src_len=0;
        int i=0;
     
        src_len=strlen(src);//Recupere la longueur de la chaine de depart
     
        dst=src;
     
        while((i<src_len)&&(src[i]==' '))
        {
             *dst++;//Place le pointeur dst au debut de la chaine sans espace
             i++;
        }
     
        i=src_len-2;//-2 pour eviter le caractere \0
     
        while((i>=0)&&(src[i]==' '))
        {
             src[i]='\0';
             i--;
        }
       return (dst);
    }
     
    int mysql_exec_sql(MYSQL *mysql,const char *create_definition)
    {
       return mysql_real_query(mysql,create_definition,strlen(create_definition));
    }
     
    char *str_replace(char * t1, char * t2, char * t6){
        	char*t4;
        	char*t5=malloc(0);
     
     
            	while(strstr(t6,t1)){
            		t4=strstr(t6,t1);
            		strncpy(t5+strlen(t5),t6,t4-t6);
            		strcat(t5,t2);
            		t4+=strlen(t1);
            		t6=t4;
            	}
            	return strcat(t5,t4);
    }
     
    int  treat_image(char *path, char *image_filename, char *filename, char *thumb, char *make1, char *model1, char *year1)
    {
    	char path_final[500];
     
    	sprintf(path_final, "%s/%s", path, filename);
     
    	if (fopen(image_filename, "r") != NULL)
    		{
    		//file3 == path_final
    		resizeImage_edit(path_final, image_filename);
    	//	resizeThumbnailImage_edit($thumb_image_location, $file3,$w,$h,$x1,$y1,$scale);
    		}
     
    }
     
    int  check_directory(char *image_filename, char *filename, char *thumb, char *make1, char *model1, char *year1)
    {
    	//char *make;
    	//char *model;
    	//char *year;
    	//char dealer_image_path[500];
    	char str[500];
    	char path1[500];
    	char path2[500];
    	char path3[500];
    	char path4[500];
    	//char *path1_1 = path1;
    	char *path2_1 = path2;
    	int  z;
     
    	sprintf(str,"%s%s/%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1, model1);
    	sprintf(path1,"%s%s/%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1, model1);
    	sprintf(path2,"%s%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1);
    	sprintf(path3,"%s%s", "../images/photosAutopress/Anza_Motor_Company/", make1);
    	sprintf(path4,"%s", "../images/photosAutopress/Anza_Motor_Company/");
     
    	while (z != 1)
    	{
    		z  =check_dir(path1, path2, path3, path4);
    		if (z == 1)
    			treat_image(path1, image_filename, filename, thumb, make1, model1, year1);
    	}
     
    	//printf("%d", z);
    	//exit(0);
    }
     
    int  import_image(char *image_filename, char *make1, char *model1, char *year1)
    {
    	printf("%s\n", image_filename);
    	printf("%s\n", make1);
    	printf("%s\n", model1);
    	printf("%s\n", year1);
    	char *make;
    	char *model;
    	char *year;
    	char *file = "car_id";
    	char thumb[100];
    	char id[50];
    	char filex[15];
    	int d;
     
    	MYSQL mysql;
     
    	//unsigned int num_fields;
    	//unsigned int i;
    	//char query_def[1000];
     
    	make = str_replace("\"", "", make1);
    	model = str_replace("\"", "", model1);
    	year = str_replace("\"", "", year1);
     
     
    	//start mysql sequence to get the last id of the car inserted...
    	if(mysql_init(&mysql)==NULL)
    	{
    		printf("\nFailed to initate MySQL connection");
    		exit(1);
    	}
     
    	if(!mysql_real_connect (&mysql,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql));
    			 mysql_close(&mysql);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql,"SELECT t1.car_id, t1.car_make, t1.car_model, t1.car_year, t2.car_dealer_id FROM tbl_cars AS t1 LEFT JOIN tbl_cars_details AS t2 ON t1.car_id = t2.car_id ORDER BY t1.car_id DESC LIMIT 1"))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql));
    				 mysql_close(&mysql);
    				 exit(0);
    		}
    		else
    		{
    			MYSQL_RES *result;
    			result = mysql_use_result(&mysql);
     
    			if (result)  // there are rows
    			{
     
    				// retrieve rows, then call mysql_free_result(result)
    				MYSQL_ROW row;
    				if((row = mysql_fetch_row(result)))
    				{
    					unsigned long *lengths = mysql_fetch_lengths(result);
    					if(lengths[0] > 0)
     
    					{
    					  //printf("%s\n", row[0]);
    /*
    					  //build the new image file name like car_id213123_45645646_large.jpg and car_id213123_45645646.jpg
    					  int i;
    					  char ran[11];
    					  i = rand();
    					  sprintf(ran, "%d", i);
    					  strcpy(id, row[0]);
    					  d = strlen(id);
    					  strcat(filex, file);
    					  strcat(filex, id);
    					  strcat(filex, "_");
    					  strcat(filex, ran);
    					  strcpy(thumb, filex);
    					  strcat(filex, "_large.jpg");
    					  strcat(thumb, ".jpg");
    					  //all good? check the directory...
    					 // check_directory(image_filename, filex, thumb, make, model, year);
    */
    					  return (1);
     
    					}
     
    				}
    				//mysql_free_row(row);
    				mysql_free_result(result);
    			}
    			else
    			{			
    				mysql_free_result(result);
    				return (0);
    			}
    		}
     
    		mysql_close(&mysql);
    		//free(*mysql);
    		return(1);
     
    }
     
    char *loop_check_images(char *id, int p)
    {
    	int length_id;
    	int length_id2;
    	char *str_test;
    	char *str_test1;
    	char *str_test2;
    	char *blah;
    	char buf[10];
      	int  z;
    	char *test;
    	char *test1;
      	FILE *fp1;
     
    	length_id = strlen(id);
    	length_id2 = length_id + 6;
     
    	//length_id++;
     
    	//for (x=1;x<=10;x++)
    	//{
     
    	str_test = (char *)malloc(sizeof(char [length_id+1]));
    	blah = str_replace("\"", "", id);
    	test = "./";
    	str_test = strcat(blah, "-");
    	z = strlen(str_test);
    	test1 = (char *)malloc(sizeof(char [z+3]));
    	test1 = strcat(test1, "./");
    	test1 = strcat(test1, str_test);
    	z = strlen(test1);
     
    		str_test1 = (char *)malloc(sizeof(char [z+5]));
    		sprintf(buf, "%d", p);
    		//printf("%s\n", buf);
    		str_test1 = strcat(test1, buf);
    		str_test2 = strcat(str_test1, ".JPG");
    		//Use fopen to see if the file exist, if NULL doesnt exist
      		fp1=fopen(str_test2,"r");
    		if (fp1 == NULL)
    		{
    			str_test2 = "";
    			return (str_test2);
    			//treat if image doesnt exist
    		  	//printf("%s %s\n", str_test2, "image doesn't exist");
    		}
    		else
    			return (str_test2);	
     
    	//}
    	//exit(0);
    }
     
    int	insert_details_in_db(char *cc, char *transmission, char *color, char *description, char *description1, char *id)
    {
    	char	query2[500];
    	MYSQL mysql2;
    	mysql_init(&mysql2);
    	char *dealer_id = "78";
    	char *used = "1";
    	char *description2;
     
    	description2 = strcat(description, description1);
     
    	sprintf(query2,"INSERT INTO tbl_cars_details (`car_dealer_id`, `car_description`, `car_used`, `car_dealer_stock_number`) VALUES (%s, '%s', '%s', %s)", dealer_id, description2, used, id);
     
    	if(!mysql_real_connect (&mysql2,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql2));
    			 mysql_close(&mysql2);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql2,query2))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql2));
    				 mysql_close(&mysql2);
    				 exit(0);
    		}
    		else
    			return 1;
    	mysql_close(&mysql2);
     
    }
     
     
    int	insert_file_in_db(char *make, char *model, char *year, char *price)
    {
    	char	query[500];
    	MYSQL mysql;
    	mysql_init(&mysql);
    	char *category = "5";
     
    	sprintf(query,"INSERT INTO tbl_cars (`car_make`, `car_model`, `car_year`, `car_price`, `car_category`) VALUES (%s, %s, %s, %s, %s)", make, model, year, price, category);
     
    	if(!mysql_real_connect (&mysql,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql));
    			 mysql_close(&mysql);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql,query))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql));
    				 mysql_close(&mysql);
    				 exit(0);
    		}
    		else
    			return 1;
     
    	mysql_close(&mysql);
     
    }
     
    int main()
    {
      char input_line[LBUFSIZ];
      char *str_image_filename;
     
      struct {
        char id[50];
        char make[50];
        char model[50];
        char model4[50];
        char color[50];
        char year[4];
        char empty[5];
        char transmission[15];
        char price[8];
        char description[400];
        char description1[400];
        char cc[6];
     
      }car_details[2500];
      FILE *fp;
      //char buf[500];
      char *model5;
      char *id;
      char *make;
      char *model;
      char *empty;
      char *year;
      char *color;
      char *transmission;
      char *description;
      char *description1;
      char *cc;
      char *price;
      char *model2;
     
      int x;
      int y;
      int i = 0, j;
     
      fp=fopen("Anzabike.dat","r");
      if (fp == NULL)
      {
    	//The file is always here but anyway...
    	printf("%s", "what happend to the file?");
    	exit(0);
    	 // gerer cette erreur d'ouverture de fichier
      }
      while (fgets(input_line, sizeof input_line, fp))
      { 
     
        id = strtok(input_line, ",");
        make = strtok(NULL, ",");
        model = strtok(NULL, ",");
        model2 = strtok(NULL, ",");
        year = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        transmission = strtok(NULL, ",");
        color = strtok(NULL, ",");
        price = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        description = strtok(NULL, ",");
        description1 = strtok(NULL, "\"");
        empty = strtok(NULL, ",");
        cc = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        strcpy(car_details[i].id, id);
        strcpy(car_details[i].make, make);
        strcpy(car_details[i].model, model);
        strcpy(car_details[i].year, year);
        strcpy(car_details[i].transmission, transmission);
        strcpy(car_details[i].color, color);
        strcpy(car_details[i].price, price);
        strcpy(car_details[i].description, description);
        strcpy(car_details[i].description1, description1);
        strcpy(car_details[i].cc, cc);
        strcpy(car_details[i].model4, model2);
        ++i;
      }
     
      //d = strlen(car_details);
      int p;
      int m;
      for (j=0; j<i; j++)
      {
        model5 = strdup(car_details[j].model);
        if (strcmp(model5, "\" \"") == 0)
    	{
    	    strcat(model5, car_details[j].model4);
    	   model5 = str_replace("\" \"", "", model5);
    	}
    	//model5 = str_replace(" ", "", model5);
        		//printf("%s\n", model5);
        x = insert_file_in_db(car_details[j].make, car_details[j].model, car_details[j].year, car_details[j].price);
        if (x = 1)
    	    y = insert_details_in_db(car_details[j].cc, car_details[j].transmission, car_details[j].color, car_details[j].description, car_details[j].description1, car_details[j].id);
    	if (y = 1)
    	{
    		// import the images
    		for (p=0;p<=3;p++)
    		{
    			str_image_filename = loop_check_images(car_details[j].id, p);
    			if (strcmp(str_image_filename, "") != 0)
    			  {
    				m = import_image(str_image_filename, car_details[j].make, model5, car_details[j].year);
    				if (m == 1)
    					printf("%s %s", str_image_filename, "image importe");
    			  }
    		}
     
    	}
     
     }
     
      return 0;
    }

  15. #15
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    613
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 613
    Par défaut
    Tu as pas oublié le main ?
    édite ton message et met le code complet
    Avant, corrige ca :
    parse.c:112: warning: implicit declaration of function ‘mkdir’

    pour cela regarde le man :
    man 2 mkdir
    Tu verras qu'il faut ajouter :
    #include <sys/stat.h>
    #include <sys/types.h>

  16. #16
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    en effet les includes enleve les warnings.
    C'est le loop avec la requete sql select qui plante.Il fait le premier mais pas les autres, il y a un probleme de malloc sur une des variables dans cette fonction.

  17. #17
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    613
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 613
    Par défaut
    Tu as encore beaucoup d'avertissement. Tu ne les comprends pas ?
    Par exemple tu en as sur :
    if (x = 1)
    et
    if (y = 1)
    Je pense que tu voulais plutôt dire == et pas = non ?
    Corrige tous les avertissements, c'est la base pour avoir un code correct.

    Il t'averti aussi que la fonction check_directory ne renvoie pas de valeur, alors qu'elle devrai renvoyer un int

    Corrige ça et reposte ton code que je puisse enfin le tester correctement

  18. #18
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
     
    //how to compile gcc -Wall -lmysqlclient -lgd -o parse parse2.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <mysql/mysql.h>
    #include <gd.h>
    #include <syslog.h>/*syslog*/
    #include <sys/stat.h>
    #include <sys/types.h>
     
    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif
     
    #ifndef WIN32
      #include <unistd.h>
    #endif
     
    #define LBUFSIZ 1000
     
    char *resizeImage_edit(char *large_image_name, char *image){
    	/*
    	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
     
    	$source = imagecreatefromjpeg($image);
     
    	imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width_orig,$height_orig);
     
    	imagejpeg($newImage,$large_image_name,85);
     
    	chmod($large_image_name, 0777);
     
    	return $large_image_name;*/
     
    	int newImageWidth = 312;
     
    	int newImageHeight = 225;
    	int width;
    	int height;
    	gdImagePtr im;
    	gdImagePtr im2;
    //	gdImagePtr im3;
    	FILE *in;
    	in = fopen(image, "r");
    	FILE *out;
    	out = fopen(large_image_name, "w");
     
    	//get original width and heights...
    	im = gdImageCreateFromJpeg(in);
     
    	width = gdImageSX(im);
    	height = gdImageSY(im);
     
    	//create new image
    	im2 = gdImageCreateTrueColor(newImageWidth, newImageHeight); //im2 == newImage
     
    	//imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width_orig,$height_orig);
    	gdImageCopyResampled(im2,im,0,0,0,0, newImageWidth,newImageHeight,width,height);
     
    	gdImageJpeg(im2,out,100);
     
    	fclose(in);
    /*	fclose(out);
    	gdImageDestroy(im);
    	gdImageDestroy(out);
    */
     
    	//chmod(large_image_name, 0777);*/
     
    	return large_image_name;
     
    }
     
    int check_dir(char *path1, char *path2, char *path3, char *path4) {
     
    /* Trim and clean white space...todo
    		$tab['make'] = trim($tab['make']);
    		$tab['model'] = trim($tab['model']);
     
    		$make = str_replace(' ', '_', $tab['make']);
     
    		$model = str_replace(' ', '_', $tab['model']);
     
    		$path_to_car = '../images/photosAutopress/Anza_Motor_Company/' . $make . '/' .$tab['year'] . '/' . $model;
     
    		$path_to_car2 = '../images/photosAutopress/Anza_Motor_Company/' . $make . '/' .$tab['year'];
     
    		$path_to_car3 = '../images/photosAutopress/Anza_Motor_Company/' . $make;
     
    		$path_to_car4 = '../images/photosAutopress/Anza_Motor_Company';*/
     
     
    		//for windobe
    		//_mkdir( path.c_str() ); 
     
     
    		if (fopen(path1,"r") == NULL)
     
    		{
     
    			if (fopen(path2, "r") == NULL)
     
    				{
     
    				if (fopen(path3, "r") == NULL)
     
    					{
     
    					if (fopen(path4, "r") == NULL)
     
    						mkdir(path4, 0777);
     
    					else 
     
    						mkdir(path3, 0777);
     
    					}	
     
    				else
     
    					mkdir(path2, 0777);
     
    				}
     
    			else 
    				{
     
    				mkdir(path1, 0777);
    				}
     
    		}
     
    		if (fopen(path1, "r") != NULL) {
     
    			return 1;
     
    		}
     
    		else
     
    		{
     
    			return 0;
     
    		}
     
     
     
    }
     
    char *strip(char *src,char *dst)
    {
        int src_len=0;
        int i=0;
     
        src_len=strlen(src);//Recupere la longueur de la chaine de depart
     
        dst=src;
     
        while((i<src_len)&&(src[i]==' '))
        {
             *dst++;//Place le pointeur dst au debut de la chaine sans espace
             i++;
        }
     
        i=src_len-2;//-2 pour eviter le caractere \0
     
        while((i>=0)&&(src[i]==' '))
        {
             src[i]='\0';
             i--;
        }
       return (dst);
    }
     
    int mysql_exec_sql(MYSQL *mysql,const char *create_definition)
    {
       return mysql_real_query(mysql,create_definition,strlen(create_definition));
    }
     
    char *str_replace(char * t1, char * t2, char * t6){
        	char*t4;
        	char*t5=malloc(0);
     
     
            	while(strstr(t6,t1)){
            		t4=strstr(t6,t1);
            		strncpy(t5+strlen(t5),t6,t4-t6);
            		strcat(t5,t2);
            		t4+=strlen(t1);
            		t6=t4;
            	}
            	return strcat(t5,t4);
    }
     
    int  treat_image(char *path, char *image_filename, char *filename, char *thumb, char *make1, char *model1, char *year1)
    {
    	char path_final[500];
     
    	sprintf(path_final, "%s/%s", path, filename);
     
    	if (fopen(image_filename, "r") != NULL)
    		{
    		//file3 == path_final
    		resizeImage_edit(path_final, image_filename);
    	//	resizeThumbnailImage_edit($thumb_image_location, $file3,$w,$h,$x1,$y1,$scale);
    		}
    	return (0);
     
    }
     
    int  check_directory(char *image_filename, char *filename, char *thumb, char *make1, char *model1, char *year1)
    {
     
    	char str[500];
    	char path1[500];
    	char path2[500];
    	char path3[500];
    	char path4[500];
     
    	int  z;
     
    	sprintf(str,"%s%s/%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1, model1);
    	sprintf(path1,"%s%s/%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1, model1);
    	sprintf(path2,"%s%s/%s", "../images/photosAutopress/Anza_Motor_Company/", make1, year1);
    	sprintf(path3,"%s%s", "../images/photosAutopress/Anza_Motor_Company/", make1);
    	sprintf(path4,"%s", "../images/photosAutopress/Anza_Motor_Company/");
     
    	while (z != 1)
    	{
    		z  =check_dir(path1, path2, path3, path4);
    		if (z == 1)
    			treat_image(path1, image_filename, filename, thumb, make1, model1, year1);
    	}
     
    	return (0);
    }
     
    int  import_image(char *image_filename, char *make1, char *model1, char *year1)
    {
    	printf("%s\n", image_filename);
    	printf("%s\n", make1);
    	printf("%s\n", model1);
    	printf("%s\n", year1);
    	char *make;
    	char *model;
    	char *year;
     
    	MYSQL mysql;
     
    	//unsigned int num_fields;
    	//unsigned int i;
    	//char query_def[1000];
     
    	make = str_replace("\"", "", make1);
    	model = str_replace("\"", "", model1);
    	year = str_replace("\"", "", year1);
     
     
    	//start mysql sequence to get the last id of the car inserted...
    	if(mysql_init(&mysql)==NULL)
    	{
    		printf("\nFailed to initate MySQL connection");
    		exit(1);
    	}
     
    	if(!mysql_real_connect (&mysql,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql));
    			 mysql_close(&mysql);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql,"SELECT t1.car_id, t1.car_make, t1.car_model, t1.car_year, t2.car_dealer_id FROM tbl_cars AS t1 LEFT JOIN tbl_cars_details AS t2 ON t1.car_id = t2.car_id ORDER BY t1.car_id DESC LIMIT 1"))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql));
    				 mysql_close(&mysql);
    				 exit(0);
    		}
    		else
    		{
    			MYSQL_RES *result;
    			result = mysql_use_result(&mysql);
     
    			if (result)  // there are rows
    			{
     
    				// retrieve rows, then call mysql_free_result(result)
    				MYSQL_ROW row;
    				if((row = mysql_fetch_row(result)))
    				{
    					unsigned long *lengths = mysql_fetch_lengths(result);
    					if(lengths[0] > 0)
     
    					{
    					  //printf("%s\n", row[0]);
    /*
    					  //build the new image file name like car_id213123_45645646_large.jpg and car_id213123_45645646.jpg
    					  int i;
    					  char ran[11];
    					  i = rand();
    					  sprintf(ran, "%d", i);
    					  strcpy(id, row[0]);
    					  d = strlen(id);
    					  strcat(filex, file);
    					  strcat(filex, id);
    					  strcat(filex, "_");
    					  strcat(filex, ran);
    					  strcpy(thumb, filex);
    					  strcat(filex, "_large.jpg");
    					  strcat(thumb, ".jpg");
    					  //all good? check the directory...
    					 // check_directory(image_filename, filex, thumb, make, model, year);
    */
    					  return (1);
     
    					}
     
    				}
    				//mysql_free_row(row);
    				mysql_free_result(result);
    			}
    			else
    			{			
    				mysql_free_result(result);
    				return (0);
    			}
    		}
     
    		mysql_close(&mysql);
    		//free(*mysql);
    		return(1);
     
    }
     
    char *loop_check_images(char *id, int p)
    {
    	int length_id;
    	int length_id2;
    	char *str_test;
    	char *str_test1;
    	char *str_test2;
    	char *blah;
    	char buf[10];
      	int  z;
    	char *test;
    	char *test1;
      	FILE *fp1;
     
    	length_id = strlen(id);
    	length_id2 = length_id + 6;
     
    	//length_id++;
     
    	//for (x=1;x<=10;x++)
    	//{
     
    	str_test = (char *)malloc(sizeof(char [length_id+1]));
    	blah = str_replace("\"", "", id);
    	test = "./";
    	str_test = strcat(blah, "-");
    	z = strlen(str_test);
    	test1 = (char *)malloc(sizeof(char [z+3]));
    	test1 = strcat(test1, "./");
    	test1 = strcat(test1, str_test);
    	z = strlen(test1);
     
    		str_test1 = (char *)malloc(sizeof(char [z+5]));
    		sprintf(buf, "%d", p);
    		//printf("%s\n", buf);
    		str_test1 = strcat(test1, buf);
    		str_test2 = strcat(str_test1, ".JPG");
    		//Use fopen to see if the file exist, if NULL doesnt exist
      		fp1=fopen(str_test2,"r");
    		if (fp1 == NULL)
    		{
    			str_test2 = "";
    			return (str_test2);
    			//treat if image doesnt exist
    		  	//printf("%s %s\n", str_test2, "image doesn't exist");
    		}
    		else
    			return (str_test2);	
     
    	//}
    	//exit(0);
    }
     
    int	insert_details_in_db(char *cc, char *transmission, char *color, char *description, char *description1, char *id)
    {
    	char	query2[500];
    	MYSQL mysql2;
    	mysql_init(&mysql2);
    	char *dealer_id = "78";
    	char *used = "1";
    	char *description2;
     
    	description2 = strcat(description, description1);
     
    	sprintf(query2,"INSERT INTO tbl_cars_details (`car_dealer_id`, `car_description`, `car_used`, `car_dealer_stock_number`) VALUES (%s, '%s', '%s', %s)", dealer_id, description2, used, id);
     
    	if(!mysql_real_connect (&mysql2,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql2));
    			 mysql_close(&mysql2);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql2,query2))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql2));
    				 mysql_close(&mysql2);
    				 exit(0);
    		}
    		else
    			return 1;
    	mysql_close(&mysql2);
     
    }
     
     
    int	insert_file_in_db(char *make, char *model, char *year, char *price)
    {
    	char	query[500];
    	MYSQL mysql;
    	mysql_init(&mysql);
    	char *category = "5";
     
    	sprintf(query,"INSERT INTO tbl_cars (`car_make`, `car_model`, `car_year`, `car_price`, `car_category`) VALUES (%s, %s, %s, %s, %s)", make, model, year, price, category);
     
    	if(!mysql_real_connect (&mysql,"","root","","test_import",0,NULL,0))
    	{
    			 syslog(LOG_CONS,"%s->%s","MySQL Connect Error:",mysql_error(&mysql));
    			 mysql_close(&mysql);
    			 exit(0);
    	}
     
    		if(mysql_query(&mysql,query))
    		{
    				 syslog(LOG_CONS,"%s->%s","MySQL Query Eorror:",mysql_error(&mysql));
    				 mysql_close(&mysql);
    				 exit(0);
    		}
    		else
    			return 1;
     
    	mysql_close(&mysql);
     
    }
     
    int main()
    {
      char input_line[LBUFSIZ];
      char *str_image_filename;
     
      struct {
        char id[50];
        char make[50];
        char model[50];
        char model4[50];
        char color[50];
        char year[4];
        char empty[5];
        char transmission[15];
        char price[8];
        char description[400];
        char description1[400];
        char cc[6];
     
      }car_details[2500];
      FILE *fp;
      //char buf[500];
      char *model5;
      char *id;
      char *make;
      char *model;
      char *empty;
      char *year;
      char *color;
      char *transmission;
      char *description;
      char *description1;
      char *cc;
      char *price;
      char *model2;
     
      int x;
      int y;
      int i = 0, j;
     
      fp=fopen("Anzabike.dat","r");
      if (fp == NULL)
      {
    	//The file is always here but anyway...
    	printf("%s", "what happend to the file?");
    	exit(0);
    	 // gerer cette erreur d'ouverture de fichier
      }
      while (fgets(input_line, sizeof input_line, fp))
      { 
     
        id = strtok(input_line, ",");
        make = strtok(NULL, ",");
        model = strtok(NULL, ",");
        model2 = strtok(NULL, ",");
        year = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        transmission = strtok(NULL, ",");
        color = strtok(NULL, ",");
        price = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        description = strtok(NULL, ",");
        description1 = strtok(NULL, "\"");
        empty = strtok(NULL, ",");
        cc = strtok(NULL, ",");
        empty = strtok(NULL, ",");
        strcpy(car_details[i].id, id);
        strcpy(car_details[i].make, make);
        strcpy(car_details[i].model, model);
        strcpy(car_details[i].year, year);
        strcpy(car_details[i].transmission, transmission);
        strcpy(car_details[i].color, color);
        strcpy(car_details[i].price, price);
        strcpy(car_details[i].description, description);
        strcpy(car_details[i].description1, description1);
        strcpy(car_details[i].cc, cc);
        strcpy(car_details[i].model4, model2);
        ++i;
      }
     
      //d = strlen(car_details);
      int p;
      int m;
      for (j=0; j<i; j++)
      {
        model5 = strdup(car_details[j].model);
        if (strcmp(model5, "\" \"") == 0)
    	{
    	    strcat(model5, car_details[j].model4);
    	   model5 = str_replace("\" \"", "", model5);
    	}
    	//model5 = str_replace(" ", "", model5);
        		//printf("%s\n", model5);
        x = insert_file_in_db(car_details[j].make, car_details[j].model, car_details[j].year, car_details[j].price);
        if (x == 1)
    	    y = insert_details_in_db(car_details[j].cc, car_details[j].transmission, car_details[j].color, car_details[j].description, car_details[j].description1, car_details[j].id);
    	if (y == 1)
    	{
    		// import the images
    		for (p=0;p<=3;p++)
    		{
    			str_image_filename = loop_check_images(car_details[j].id, p);
    			if (strcmp(str_image_filename, "") != 0)
    			  {
    				m = import_image(str_image_filename, car_details[j].make, model5, car_details[j].year);
    				if (m == 1)
    					printf("%s %s", str_image_filename, "image importe");
    			  }
    		}
     
    	}
     
     }
     
      return 0;
    }
    tjs la meme..

  19. #19
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    613
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 613
    Par défaut
    J'ai compilé ton dernier code.
    J'ai renommé le fichier Anzabike2.txt que tu avait posté en Anzabike.dat
    J'ai executé, et le programme n'a pas planté...
    Quel est le message d'erreur chez toi ?

  20. #20
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 289
    Par défaut
    yelo@mahina:~/C_import_soft$ ./my_import
    ./15057-1.JPG
    "BUELL"
    "XB12R"
    "2008"
    *** glibc detected *** ./my_import: malloc(): memory corruption: 0x0806e558 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb7c05356]
    /lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7c06cad]
    ./my_import[0x80495ca]
    ./my_import[0x804a239]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7baf450]
    ./my_import[0x8048c41]
    ======= Memory map: ========
    08048000-0804b000 r-xp 00000000 08:01 730208 /home/yelo/C_import_soft/my_import
    0804b000-0804c000 rw-p 00002000 08:01 730208 /home/yelo/C_import_soft/my_import
    0804c000-0808f000 rw-p 0804c000 00:00 0 [heap]
    b7700000-b7721000 rw-p b7700000 00:00 0
    b7721000-b7800000 ---p b7721000 00:00 0
    b78c8000-b78d1000 r-xp 00000000 08:01 435209 /lib/tls/i686/cmov/libnss_files-2.7.so
    b78d1000-b78d3000 rw-p 00008000 08:01 435209 /lib/tls/i686/cmov/libnss_files-2.7.so
    b78e1000-b78e2000 rw-p b78e1000 00:00 0
    b78e2000-b78e6000 r-xp 00000000 08:01 66876 /usr/lib/libXdmcp.so.6.0.0
    b78e6000-b78e7000 rw-p 00003000 08:01 66876 /usr/lib/libXdmcp.so.6.0.0
    b78e7000-b78e8000 rw-p b78e7000 00:00 0
    b78e8000-b78ea000 r-xp 00000000 08:01 66865 /usr/lib/libXau.so.6.0.0
    b78ea000-b78eb000 rw-p 00001000 08:01 66865 /usr/lib/libXau.so.6.0.0
    b78eb000-b790a000 r-xp 00000000 08:01 67102 /usr/lib/libexpat.so.1.5.2
    b790a000-b790c000 rw-p 0001e000 08:01 67102 /usr/lib/libexpat.so.1.5.2
    b790c000-b790e000 r-xp 00000000 08:01 435203 /lib/tls/i686/cmov/libdl-2.7.so
    b790e000-b7910000 rw-p 00001000 08:01 435203 /lib/tls/i686/cmov/libdl-2.7.so
    b7910000-b7927000 r-xp 00000000 08:01 67706 /usr/lib/libxcb.so.1.0.0
    b7927000-b7928000 rw-p 00016000 08:01 67706 /usr/lib/libxcb.so.1.0.0
    b7928000-b7929000 r-xp 00000000 08:01 67704 /usr/lib/libxcb-xlib.so.0.0.0
    b7929000-b792a000 rw-p 00000000 08:01 67704 /usr/lib/libxcb-xlib.so.0.0.0
    b792a000-b794c000 r-xp 00000000 08:01 67538 /usr/lib/libpng12.so.0.15.0
    b794c000-b794d000 rw-p 00022000 08:01 67538 /usr/lib/libpng12.so.0.15.0
    b794d000-b794e000 rw-p b794d000 00:00 0
    b794e000-b79b8000 r-xp 00000000 08:01 68609 /usr/lib/libfreetype.so.6.3.16
    b79b8000-b79bb000 rw-p 0006a000 08:01 68609 /usr/lib/libfreetype.so.6.3.16
    b79bb000-b79e4000 r-xp 00000000 08:01 67110 /usr/lib/libfontconfig.so.1.3.0
    b79e4000-b79e5000 rw-p 00029000 08:01 67110 /usr/lib/libfontconfig.so.1.3.0
    b79e5000-b7a04000 r-xp 00000000 08:01 67395 /usr/lib/libjpeg.so.62.0.0
    b7a04000-b7a05000 rw-p 0001e000 08:01 67395 /usr/lib/libjpeg.so.62.0.0
    b7a05000-b7ae9000 r-xp 00000000 08:01 66859 /usr/lib/libX11.so.6.2.0
    b7ae9000-b7aec000 rw-p 000e4000 08:01 66859 /usr/lib/libX11.so.6.2.0
    b7aec000-b7afb000 r-xp 00000000 08:01 66898 /usr/lib/libXpm.so.4.11.0
    b7afb000-b7afc000 rw-p 0000f000 08:01 66898 /usr/lib/libXpm.so.4.11.0
    b7afc000-b7afd000 rw-p b7afc000 00:00 0
    b7afd000-b7b11000 r-xp 00000000 08:01 67718 /usr/lib/libz.so.1.2.3.3
    b7b11000-b7b12000 rw-p 00013000 08:01 67718 /usr/lib/libz.so.1.2.3.3
    b7b12000-b7b35000 r-xp 00000000 08:01 435204 /lib/tls/i686/cmov/libm-2.7.so
    b7b35000-b7b37000 rw-p 00023000 08:01 435204 /lib/tls/i686/cmov/libm-2.7.so
    b7b37000-b7b4b000 r-xp 00000000 08:01 435206 /lib/tls/i686/cmov/libnsl-2.7.so
    b7b4b000-b7b4d000 rw-p 00013000 08:01 435206 /lib/tls/i686/cmov/libnsl-2.7.so
    b7b4d000-b7b4f000 rw-p b7b4d000 00:00 0
    b7b4f000-b7b58000 r-xp 00000000 08:01 435202 /lib/tls/i686/cmov/libcrypt-2.7.so
    b7b58000-b7b5a000 rw-p 00008000 08:01 435202 /lib/tls/i686/cmov/libcrypt-2.7.so
    b7b5a000-b7b81000 rw-p b7b5a000 00:00 0
    b7b81000-b7b95000 r-xp 00000000 08:01 435214 /lib/tls/i686/cmov/libpthread-2.7.so
    b7b95000-b7b97000 rw-p 00013000 08:01 435214 /lib/tls/i686/cmov/libpthread-2.7.so
    b7b97000-b7b99000 rw-p b7b97000 00:00 0
    b7b99000-b7ce2000 r-xp 00000000 08:01 435200 /lib/tls/i686/cmov/libc-2.7.so
    b7ce2000-b7ce3000 r--p 00149000 08:01 435200 /lib/tls/i686/cmov/libc-2.7.so
    b7ce3000-b7ce5000 rw-p 0014a000 08:01 435200 /lib/tls/i686/cmov/libc-2.7.so
    b7ce5000-b7ce9000 rw-p b7ce5000 00:00 0
    b7ce9000-b7d06000 r-xp 00000000 0./15057-1.JPG image importeAborted

Discussions similaires

  1. Ouvrir plusieurs fichier .txt dans une loop
    Par IngenieurElec dans le forum C
    Réponses: 5
    Dernier message: 16/11/2012, 11h54
  2. Utiliser .click() dans une loop
    Par doobinay dans le forum jQuery
    Réponses: 1
    Dernier message: 09/04/2011, 05h12
  3. Update d'un control Label dans un loop
    Par marric01 dans le forum ASP.NET Ajax
    Réponses: 1
    Dernier message: 05/08/2010, 16h08
  4. enumerer objets dans une loop
    Par MOLOT dans le forum Calcul scientifique
    Réponses: 4
    Dernier message: 27/03/2009, 13h38
  5. Create table dans une loop PL/SQL
    Par neo.51 dans le forum PL/SQL
    Réponses: 5
    Dernier message: 04/03/2009, 10h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo