473,230 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

How to read file

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
0 30968

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

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.