473,394 Members | 1,737 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,394 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 30973

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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.