1421
Конференция C SoobCha
письмо # 1422
|
1423 |
|
Hi All,Sorry for non-russian, I haven't russian locale just now. Check, it should work. I don't have your data files, so I can't check but I didn't change a logic, I just remove some bugs. Note: compiled by gcc version 4.2.2 (Gentoo 4.2.2 p1.0) but I think gcc 3.x will compile it too. -------------------------------------------cut----------------------------------------- #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <ctype.h> double extractor (char* u_name) { double val=-1; FILE *fp = NULL; /* ----- initialization ----- */ const size_t max_fullstr_len = 128; char fullstr[max_fullstr_len]; memset(fullstr, 0, sizeof(fullstr)); fullstr[0]='C'; fullstr[1]='h'; fullstr[2]='a'; fullstr[3]='i'; fullstr[4]='n'; fullstr[5]=' '; size_t u_name_len=strlen(u_name); if (u_name_len - 9 > max_fullstr_len) // don't forget null termination { printf("[ERR] fullstr is too long (%d).\n",u_name_len); return -1; } strncpy(&fullstr[6], u_name, u_name_len); fullstr[u_name_len+6] = ' '; fullstr[u_name_len+7] = '('; fp=fopen ("/tmp/chains","r"); if (!fp) { printf("[ERR] Can't open /tmp/chains for read.\n"); return -1; } /* ----- initialisation ----- */ /* ----- read file ----- */ while (!feof(fp)) { const size_t data_len = 256; char data[data_len+1]; memset(data, 0, sizeof(data)); fgets (data, data_len, fp); size_t fullstr_len = strlen(fullstr); if (strncmp(data, fullstr, fullstr_len)) // if data begins from fullstr... { fgets (data, data_len, fp); // skip 256 bytes or 1 line... fgets (data, data_len, fp); // and read next 256 bytes or 1 line /* data_ch |012345678901234| (see value_len) | nnnnnnnnnn| "n" is a digit */ const size_t value_len = 15; char value[value_len]; memset(value,0,sizeof(value)); int j=0,i=0; for(i=0; i<value_len; ++i) { if(isdigit(data[i])) // skip ALL non-digits { value[j]= data[i]; j++; } } val = atol(value); break; } } /* ----- read file ----- */ if (fp) fclose(fp); return val; } int main () { time_t lt = time(NULL); struct tm * ptime = localtime(<); int DAT = ptime->tm_mday; int DAT2 = ptime->tm_mon + 1; int DAT3 = ptime->tm_hour; int DAT4 = ptime->tm_min; FILE *fp = fopen("/tmp/collect_users","r"); if(!fp) { printf("[ERR] Can't open /tmp/collect_users\n."); return -1; } FILE *fp_sql = fopen("/tmp/collector_step","w"); if(!fp_sql) { printf("[ERR] Can't open /tmp/collector_step\n."); return -1; } const size_t data_len = 256; char data[data_len+1]; memset(data, 0, sizeof(data)); fgets (data, data_len, fp); fgets (data, data_len, fp); int counter = 0; while (!feof(fp)) { printf ("\n counter=%d",++counter); char *s_admin = NULL; char *s_price = NULL; char *s_name = strtok(data, "\t\n"); if(s_name) s_price = strtok(NULL, "\t\n"); if(s_price) s_admin = strtok(NULL, "\t\n"); if( !s_price || !s_name || !s_admin ) { printf("[ERR] Can't extract values.\n"); break; } double price=atof(s_price); int admin=atoi(s_admin); printf ("\nu_name=%s, price=%f, admin=%d",s_name,price,admin); long bytes=extractor(s_name); if (bytes>3) { double b2; double tmp_money; switch(admin) { case 10: break; fprintf (fp_sql,"INSERT INTO Bill_day_m VALUES (%d,%d,%d,%d,'%s',%ld,%ld);\n",DAT,DAT2,DAT3,DAT4,s_name,bytes,bytes); fprintf (fp_sql,"INSERT INTO packet_Bill_day VALUES (%d,%d,%d,%d,'%s',%ld);\n",DAT,DAT2,DAT3,DAT4,s_name,bytes); fprintf (fp_sql,"UPDATE active_packets set bytes_left=bytes_left-%ld where login='%s' and active=1;\n",bytes,s_name); case 11: fprintf (fp_sql,"INSERT INTO Bill_day_m VALUES (%d,%d,%d,%d,'%s',%ld,%ld);\n",DAT,DAT2,DAT3,DAT4,s_name,bytes,bytes); break; case 12: b2 = bytes*0.00000095367432; tmp_money = b2*price; fprintf (fp_sql,"INSERT INTO Bill_day_m VALUES (%d,%d,%d,%d,'%s',%ld,%ld);\n",DAT,DAT2,DAT3,DAT4,s_name,bytes,bytes); fprintf (fp_sql,"INSERT INTO Bill_day VALUES (%d,%d,%d,%d,'%s',%ld,%ld);\n",DAT,DAT2,DAT3,DAT4,s_name,bytes,bytes); fprintf (fp_sql,"UPDATE Balance SET money_rest=money_rest-%f WHERE login='%s';\n",tmp_money,s_name); break; default: b2 = bytes*0.00000095367432; tmp_money = b2*price; fprintf (fp_sql,"INSERT INTO Bill_day_m VALUES (%d,%d,%d,%d,'%s',%ld,%ld);\n",DAT,DAT2,DAT3,DAT4,s_name,bytes,bytes); fprintf (fp_sql,"INSERT INTO Bill_day VALUES (%d,%d,%d,%d,'%s',%ld,%ld);\n",DAT,DAT2,DAT3,DAT4,s_name,bytes,bytes); fprintf (fp_sql,"UPDATE Balance SET money_rest=money_rest-%f WHERE login='%s';\n",tmp_money,s_name); } } fgets (data, data_len, fp); } if (fp) fclose(fp); if (fp_sql) { fflush(fp_sql); fclose(fp_sql); } return 0; } ---------------------cut----------------------------------------- > vremenno vyshel iz situacii razbiv file s polzovatelyami (fp) > na neskol'ko po primerno 500 zapisey > problema ushla > nooo > zametil osobennost' pered tem kak uspel eto sdelat' > esli programma zapuskaetsya cherez cron > to ona vyletaet na 1011 zapisi > esli prosto iz konsoli to na 1020 zapisi > est' u kogo-to kakie-to mysli? > Sorry for my comments, but you ask about possible bugs: 0) you don't close a file (line 25), it was opened in extractor() - it may be a main root cause of segfault 1) you open files but never check for error, file can fail to open 2) such calls *system("date +%d > /tmp/date.time") *can fail 3) line 20 - you can write out of array's bounds 4) line 42 - you can write out of array's bounds 5) lines 74-89 - it's a poor style, I wouldn't comment... ------ Regards, Vasily. |
1421
|
1423 |
|
|
|
|