read text file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

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
  #11
Aug 2nd, 2006
Oh. Then search for passing Command Line Arguments in C/C++. By using Command Line arguments, you can pass the filename from the command line to your program.
Like this
  1. C:\> YourProgram.exe filename
then you can call this program when you receive the file.
バルサミコ酢やっぱいらへんで
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
  #12
Aug 2nd, 2006
Hi WolfPack,

I've done like this:
  1. int main()
  2. {
  3. char filename[100];
  4. scanf("%s",&filename);
  5. readLogFile(filename);
  6. }

since i'm using scanf, so..I need to give the input to my .exe file. If possible, I want it to be auto run for reading n insert the contents into database. Is there any way that I can rewrite the code so that, It can automatically read any filename that coming in.

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
  #13
Aug 2nd, 2006
Change your program like this
  1.  
  2. int main(int argc, char *argv[])
  3. {
  4. int i;
  5. if ( argc == 1 )
  6. {
  7. cout << "Enter at least one file name\n";
  8. return -1;
  9. }
  10. for (i = 1; i < argc; i++)
  11. {
  12. readLogFile(argv[i]);
  13. }
  14. return 0;
  15. }
Now when you Run the program from the command line, type the program name and the filenames like this
Program.exe Filename1.txt Filename2.txt ...
the files will get processed. I hope that answers your question.
バルサミコ酢やっぱいらへんで
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
  #14
Aug 2nd, 2006
Hi WolfPack,

ok..I've got what do u mean..and I already try it n it works..but..I still need to type the filename right?..If possible, I just want to run program.exe only but then it can read all the files inside the same directory.

I just will run:

program.exe


Thanks...you already help me a lot...your help very much appreciated...
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
  #15
Aug 2nd, 2006
Originally Posted by whitemoss
Hi WolfPack,

ok..I've got what do u mean..and I already try it n it works..but..I still need to type the filename right?..If possible, I just want to run program.exe only but then it can read all the files inside the same directory.

I just will run:

program.exe


Thanks...you already help me a lot...your help very much appreciated...
Yes, you still have to type the filename. There is no way to automatically get it done without making the program more complex. The best and easiest way to do is write a Command Script that executes your program for all the datafiles in the folder. What is your operating system?
バルサミコ酢やっぱいらへんで
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
  #16
Aug 2nd, 2006
Hi WolfPack,

oooo...ic..if that so, maybe I need to provide bash script to execute the program..im using OS Red Hat Linux...

Thanks...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: read text file

 
0
  #17
Aug 2nd, 2006
Originally Posted by whitemoss
Hi WolfPack,

ok..I've got what do u mean..and I already try it n it works..but..I still need to type the filename right?..If possible, I just want to run program.exe only but then it can read all the files inside the same directory.
This may not be necessary. If you use the filename *.fil, in the version of Unix I used the command line was expanded to include all .fil files. Then you only have to loop through the command line parameters if Red Hat works the same. Give it a try with something simple:
  1. int main(int ac, char *av[])
  2. {
  3. int parm=1;
  4. while (parn < ac)
  5. {
  6. puts(av[parm++]);
  7. }
  8. return 0;
  9. }
See what happens. Add the headers, of course.


By the way, using scanf() to read strings is just like using gets() -- you should really us something safer like fgets().
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
  #18
Aug 2nd, 2006
Originally Posted by WaltP
This may not be necessary. If you use the filename *.fil, in the version of Unix I used the command line was expanded to include all .fil files. Then you only have to loop through the command line parameters if Red Hat works the same. Give it a try with something simple:
  1. int main(int ac, char *av[])
  2. {
  3. int parm=1;
  4. while (parn < ac)
  5. {
  6. puts(av[parm++]);
  7. }
  8. return 0;
  9. }
hmm. Cool trick. pity it doesn't work in Windows too. By the way, I think this is a typo. while (parn < ac)

Edit:
Works in Cygwin. That is a consolation.
Last edited by WolfPack; Aug 2nd, 2006 at 3:02 pm.
バルサミコ酢やっぱいらへんで
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
  #19
Aug 8th, 2006
Hi All,

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

How can I alter this code so that this code will scan all files in that folder and insert it properly into table. Table structure will be something like this:
mysql> desc PostpaidProfile2;
  1. +---------------+------------------+------+-----+---------+----------------+
  2. | Field | Type | Null | Key | Default | Extra |
  3. +---------------+------------------+------+-----+---------+----------------+
  4. | RecordID | int(10) unsigned | | PRI | NULL | auto_increment |
  5. | MobileNumber | varchar(11) | YES | | NULL | |
  6. | Status | tinyint(1) | YES | | 0 | |
  7. | CreateDate | datetime | YES | | NULL | |
  8. | TerminateDate | datetime | YES | | NULL | |
  9. | PackagePlan | varchar(100) | YES | | NULL | |
  10. +---------------+------------------+------+-----+---------+----------------+

There are 2 files received every hour namely DB1-HH-DD-MM-YYYY.txt and DB2-HH-DD-MM-YYYY.txt. If this program read DB1-HH-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-HH-DD-MM-YYYY.txt which will insert "Pay-Per-Use" in the PackagePlan field. After reading those files, they will be removed and this program will wait for another files that will be coming in on the next hour.

Hopefully u guys can understand what I mean..
Last edited by Dave Sinkula; Aug 8th, 2006 at 9:18 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
  #20
Oct 3rd, 2006
Hi All,

I've written a code to append string into a file. How to modify that code so that, before the string had been appended, it should check the existing of the file. If the file does exist, then it can instruct to append the string into that particular file. But if that file doesn't exist, the program should return error . I've try to modify it but failed.I cant compile that program.

Below are my code:
  1. void writeLogFile(char line2[1600])
  2. {
  3. FILE *fp;
  4.  
  5. fp=fopen("internet_access_lookup_table.txt", "a");
  6.  
  7. if(fp==NULL) {
  8. printf("file not found!\n");
  9. exit(0);
  10. }
  11. else {
  12.  
  13. if ( (fputs(line2, fp)) == EOF) {
  14. printf("error write...");
  15. }
  16. if ( (fputs("\n", fp)) == EOF) {
  17. printf("error write1...");
  18. }
  19. }
  20.  
  21. fclose(fp);
  22.  
  23. }

Thanks in advance
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



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC