read text file

Reply

Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

read text file

 
0
  #1
Jul 31st, 2006
Hi all,

I'd posted this problem previously but in the wrong place....Hopefully, I can get the answer from this thread. I'm a newbie in C..Right now, I have to write a code to read a text file and then insert all the info inside that text file into database. Should anyone have a simple sample of it, maybe you can share with me in order for me to learn on how to do it.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: read text file

 
0
  #2
Jul 31st, 2006
Originally Posted by whitemoss
Hi all,

I'd posted this problem previously but in the wrong place....Hopefully, I can get the answer from this thread. I'm a newbie in C..Right now, I have to write a code to read a text file and then insert all the info inside that text file into database. Should anyone have a simple sample of it, maybe you can share with me in order for me to learn on how to do it.

Thanks in advance
  1. // reading a text file
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main () {
  8. string line; //this will contain the data read from the file
  9. ifstream myfile ("example.txt"); //opening the file.
  10. if (myfile.is_open()) //if the file is open
  11. {
  12. while (! myfile.eof() ) //while the end of file is NOT reached
  13. {
  14. getline (myfile,line); //get one line from the file
  15. cout << line << endl; //and output it
  16. }
  17. myfile.close(); //closing the file
  18. }
  19. else cout << "Unable to open file"; //if the file is not open output <--
  20.  
  21. return 0;
  22. }


I don't really get what you mean by sending it to a database, but this is the file-reading part


Greetz, Eddy


EDIT:
Oh wait, you sayd "C", did you mean C or C++? If it's C above code probably won't work.
If you're really doing C then I suggest you switch to C++ anyway
Last edited by Eddy Dean; Jul 31st, 2006 at 3:43 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

Re: read text file

 
0
  #3
Jul 31st, 2006
Hi Eddy,

Yerp, I need to write that code in C...n what I meant by sending it to database is that, I have 1 text file to be read, n after that, this program need to insert all the read data from that file into database. Actually, I've got that text file from other people and I need to insert that data into database so that, I can use it for my program..

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: read text file

 
0
  #4
Jul 31st, 2006
I still don't really get what you mean with the database. Is it an SQL database you need to put it to (or another type of database)? Or do you just mean an array(table) inside your own program?


Greetz, Eddy
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

Re: read text file

 
0
  #5
Jul 31st, 2006
Hi Eddy,

Actually, I want to read the file and then extract the content of the file to be inserted into the database (MySQL database). I had created a table in MySQL so that, when this C program run, it can read as well as inserting the content to that table created.
Hope you can get what I mean..

Thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: read text file

 
0
  #6
Jul 31st, 2006
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: read text file

 
0
  #7
Jul 31st, 2006
you will have to

1) learn SQL database language -- SELECT, INSERT and UPDATE are three most commonly used SQL functions.

2) learn how to use ODBC to access the database from C, C++ or most any other computer language. Here is a short tutorial to get you started.

And here are a lot of other links.
Last edited by Ancient Dragon; Jul 31st, 2006 at 8:13 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

Re: read text file

 
0
  #8
Aug 2nd, 2006
Hi all,

Basically, I have done my code and it already successfully read the text file n insert the content into the database. But the problem is..i dunt know how to write a code so that, this program can read other files at the same time.Meaning that, I dunt want the filename to be hard coded because I'll receive 4 files to be read every hour.
Below is the code:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mysql.h>
  4.  
  5. #define HOST "localhost"
  6. #define USER ""
  7. #define PASSWD ""
  8. #define DB_NAME ""
  9.  
  10. void readLogFile();
  11. void insert2DB();
  12. void logSeparator(char log[1600]);
  13. void logSeparatorDetails(char logDetails[2500]);
  14. void date2DB(char dateLog[20]);
  15. //int operatorPrefix();
  16.  
  17. static MYSQL demo_db;
  18. char string1[150], msg[250];
  19. char date[50],time[50],noXXX[50],hpno[50],noYYY[50];
  20. char dateLog[20];
  21. char dd[20], mm[20], yyyy[20], theDate[20];
  22.  
  23. int main()
  24. {
  25. readLogFile();
  26. }
  27.  
  28. void readLogFile()
  29. {
  30. char line[1600];
  31. FILE *fp;
  32.  
  33. fp=fopen("DB1-12-27-07-2006.txt", "r+");
  34.  
  35. if(fp==NULL) {
  36. printf("file not found!\n");
  37. exit(0);
  38. }
  39. else {
  40. while(!feof(fp)) {
  41. bzero(line, sizeof(line));
  42. fgets(line,1600,fp);
  43. logSeparator(line);
  44. logSeparatorDetails(string1);
  45. date2DB(date);
  46. //operatorPrefix();
  47. insert2DB();
  48. }
  49. }
  50.  
  51. fclose(fp);
  52. return;
  53. }
  54.  
  55. void insert2DB()
  56. {
  57. char query[16384];
  58. char query1[16384];
  59. int stat;
  60.  
  61. if(!mysql_connect(&demo_db, HOST, USER, PASSWD))
  62. {
  63. printf(mysql_error(&demo_db));
  64. exit(1);
  65. }
  66.  
  67. if(mysql_select_db(&demo_db, DB_NAME))
  68. {
  69. printf(mysql_error(&demo_db));
  70. exit(1);
  71. }
  72.  
  73. bzero(query, sizeof(query1));
  74. sprintf(query, "INSERT INTO PostpaidProfile2 (MobileNumber,PackagePlan,Status) VALUES('%s','%s',1)",hpno,msg,stat);
  75.  
  76. printf("%s\n",query);
  77.  
  78. if(mysql_real_query(&demo_db, query, strlen(query)+255))
  79. {
  80. printf(mysql_error(&demo_db));
  81. bzero(query, sizeof(query));
  82. exit(1);
  83. }
  84.  
  85. mysql_close(&demo_db);
  86.  
  87. return;
  88. }
  89.  
  90. void logSeparator(char log[2000])
  91. {
  92. char *temp, msgTemp[200];
  93. int io;
  94.  
  95. io=0;
  96. bzero(string1, sizeof(string1));
  97. bzero(msg, sizeof(msg));
  98.  
  99. temp = strtok (log," ");
  100.  
  101. while (temp != NULL)
  102. {
  103. io++;
  104.  
  105. switch(io)
  106. {
  107. case 1:
  108. strcpy (string1, temp);
  109. break;
  110. default:
  111. strcpy (msgTemp, temp);
  112. if (strlen(msg) != 0) {
  113. strcat(msg," ");
  114. }
  115. strcat(msg,msgTemp);
  116. break;
  117. }
  118.  
  119. temp = strtok (NULL," ");
  120. }
  121. }
  122.  
  123. void logSeparatorDetails(char logDetails[2000])
  124. {
  125. char *temp2;
  126. int io2;
  127.  
  128. io2=0;
  129. bzero(hpno, sizeof(hpno));
  130. bzero(msg, sizeof(msg));
  131.  
  132. temp2 = strtok (logDetails,",");
  133.  
  134. while (temp2 != NULL)
  135. {
  136. io2++;
  137.  
  138. switch(io2)
  139. {
  140.  
  141. case 1:
  142. strcpy (hpno, temp2);
  143. break;
  144. case 2:
  145. strcpy (msg, temp2);
  146. break;
  147.  
  148. }
  149.  
  150. temp2 = strtok (NULL,",");
  151. }
  152. }
  153.  
  154. void date2DB(char dateLog[20])
  155. {
  156. bzero(dd, sizeof(dd));
  157. bzero(mm, sizeof(mm));
  158. bzero(yyyy, sizeof(yyyy));
  159. bzero(theDate, sizeof(theDate));
  160.  
  161. strncpy(dd,dateLog,2);
  162. strncpy(mm,dateLog+2,2);
  163. strncpy(yyyy,dateLog+4,4);
  164.  
  165. strcat(theDate,yyyy);
  166. strcat(theDate,"-");
  167. strcat(theDate,mm);
  168. strcat(theDate,"-");
  169. strcat(theDate,dd); //convert to yyyy-mm-dd as in mysql date format
  170.  
  171. return;
  172. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: read text file

 
0
  #9
Aug 2nd, 2006
change readLogFile() so that you can pass the filename into it.
readLogFile( const char* filename) then you can call readLogFile for any file you want.
  1. readLogFile("file1.txt");
  2. readLogFile("file2.txt");
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

Re: read text file

 
0
  #10
Aug 2nd, 2006
Hi WolfPack,

Refer to your given code, it's work but I still need to hardcode the filename. I want it to automatically read all the files received because I'll always receive files every hour and everyday.


Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC