By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,222 Members | 1,099 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,222 IT Pros & Developers. It's quick & easy.

How to read file

P: 4
Hi All,

I had written a code to read a file and insert it's contents to the database. Since I will receive 3 files every hour, so, this program should read those files and insert the contents accordingly to the database and then after that, those files need to be removed to another folder. This program then will be executed again once it receive another 3 files on the next hour. The reason why I need to remove those files to another folder is becoz, the next 3 files will use the same name as the files before. The filename format will be:
1) DB1-DD-MM-YYYY.txt
2) DB2-DD-MM-YYYY.txt
3) DB3-DD-MM-YYYY.txt

Table structure will be:
mysql> desc PostpaidProfile2;
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| RecordID | int(10) unsigned | | PRI | NULL | auto_increment |
| MobileNumber | varchar(11) | YES | | NULL | |
| Status | tinyint(1) | YES | | 0 | |
| CreateDate | datetime | YES | | NULL | |
| TerminateDate | datetime | YES | | NULL | |
| PackagePlan | varchar(100) | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+


If this program read DB1-DD-MM-YYYY.txt, then it will insert all the contents of the file to the PostpaidProfile2 table and in the PackagePlan field, it will indicate "Unlimited" and vice versa for DB2-DD-MM-YYYY.txt which will insert "Pay-Per-Use" in the PackagePlan field.

Now, I need to alter the code so that this program will scan all files in that folder and insert it properly into table as what have been explained above.



Expand|Select|Wrap|Line Numbers
  1. #include <mysql.h>
  2.  
  3. #define HOST "localhost"
  4. #define USER "yati"
  5. #define PASSWD "yati"
  6. #define DB_NAME "subscriberTool"
  7.  
  8. void readLogFile(const char* filename);
  9. void insert2DB();
  10. void logSeparator(char log[1600]);
  11. void logSeparatorDetails(char logDetails[2500]);
  12. void date2DB(char dateLog[20]);
  13.  
  14. static MYSQL demo_db;
  15. char string1[150], msg[250];
  16. char date[50],time[50],hpno[50];
  17. char dateLog[20], filename[200];
  18. char dd[20], mm[20], yyyy[20], theDate[20];
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     int i;
  23.     if ( argc == 1 )
  24.     {
  25.             return -1;
  26.     }
  27.     for (i = 1; i < argc; i++)
  28.     {
  29.           readLogFile(argv[i]);
  30.      }
  31.     return 0;
  32. }
  33.  
  34. void readLogFile(const char* filename)
  35. {
  36.     char line[1600];
  37.     FILE *fp;
  38.  
  39.     fp=fopen(filename, "r+");
  40.  
  41.     if(fp==NULL) {
  42.         printf("file not found!\n");
  43.         exit(0);
  44.     }
  45.     else {
  46.         while(!feof(fp)) {
  47.             bzero(line, sizeof(line));
  48.             fgets(line,1600,fp);
  49.             logSeparator(line);
  50.             logSeparatorDetails(string1);
  51.             date2DB(date);
  52.             insert2DB();
  53.         }
  54.     }
  55.  
  56.     fclose(fp);
  57.     return;
  58. }
  59.  
  60. void insert2DB()
  61. {
  62.     char query[16384];
  63.     char query1[16384];
  64.     int stat;
  65.  
  66.     if(!mysql_connect(&demo_db, HOST, USER, PASSWD))
  67.       {
  68.             printf(mysql_error(&demo_db));
  69.             exit(1);
  70.       }
  71.  
  72.       if(mysql_select_db(&demo_db, DB_NAME))
  73.       {
  74.             printf(mysql_error(&demo_db));
  75.             exit(1);
  76.       }
  77.  
  78.     bzero(query, sizeof(query1));
  79.     sprintf(query, "INSERT INTO PostpaidProfile2 (MobileNumber,PackagePlan,Status) VALUES('%s','%s',1)",hpno,msg,stat);
  80.  
  81.     printf("%s\n",query);
  82.  
  83.     if(mysql_real_query(&demo_db, query, strlen(query)+255))
  84.       {
  85.            printf(mysql_error(&demo_db));
  86.             bzero(query, sizeof(query));
  87.              exit(1);
  88.       }
  89.  
  90.     mysql_close(&demo_db);
  91.  
  92.     return;
  93. }
  94.  
  95. void logSeparator(char log[2000])
  96. {
  97.     char *temp, msgTemp[200];
  98.     int io;
  99.  
  100.     io=0;
  101.     bzero(string1, sizeof(string1));
  102.     bzero(msg, sizeof(msg));
  103.  
  104.     temp = strtok (log," ");
  105.  
  106.     while (temp != NULL)
  107.         {   
  108.             io++;
  109.  
  110.                 switch(io)
  111.                 {
  112.                     case 1:
  113.                             strcpy (string1, temp);
  114.                                 break;
  115.                     default:
  116.                               strcpy (msgTemp, temp);
  117.                               if (strlen(msg) != 0) {
  118.                                   strcat(msg," ");
  119.                                 }
  120.                                 strcat(msg,msgTemp);
  121.                                 break;
  122.         }
  123.  
  124.                 temp = strtok (NULL," ");
  125.     }
  126. }
  127.  
  128. void logSeparatorDetails(char logDetails[2000])
  129. {
  130.     char *temp2;
  131.     int io2;
  132.  
  133.     io2=0;
  134.     bzero(hpno, sizeof(hpno));
  135.     bzero(msg, sizeof(msg));
  136.  
  137.     temp2 = strtok (logDetails,",");
  138.  
  139.     while (temp2 != NULL)
  140.         {   
  141.             io2++;
  142.  
  143.                 switch(io2)
  144.                 {
  145.  
  146.                     case 1:
  147.                             strcpy (hpno, temp2);
  148.                                 break;
  149.                     case 2:
  150.                             strcpy (msg, temp2);
  151.                                 break;
  152.  
  153.         }
  154.  
  155.                 temp2 = strtok (NULL,",");
  156.     }
  157. }
  158.  
  159. void date2DB(char dateLog[20])
  160. {
  161.     bzero(dd, sizeof(dd));   
  162.     bzero(mm, sizeof(mm));
  163.     bzero(yyyy, sizeof(yyyy));
  164.     bzero(theDate, sizeof(theDate));
  165.  
  166.         strncpy(dd,dateLog,2);
  167.         strncpy(mm,dateLog+2,2);
  168.         strncpy(yyyy,dateLog+4,4);
  169.  
  170.         strcat(theDate,yyyy);
  171.         strcat(theDate,"-");
  172.         strcat(theDate,mm);
  173.         strcat(theDate,"-");
  174.         strcat(theDate,dd);   //convert to yyyy-mm-dd as in mysql date format  
  175.  
  176.     return;
  177. }
  178.  
Hope u guys can help me becoz i'm quite a newbie with C

Thanks in advance
Aug 9 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.