| | |
read text file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
then you can call this program when you receive the file.
Like this
C Syntax (Toggle Plain Text)
C:\> YourProgram.exe filename
バルサミコ酢やっぱいらへんで
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
Hi WolfPack,
I've done like this:
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..
I've done like this:
C Syntax (Toggle Plain Text)
int main() { char filename[100]; scanf("%s",&filename); readLogFile(filename); }
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..
Change your program like this
Now when you Run the program from the command line, type the program name and the filenames like this
the files will get processed. I hope that answers your question.
C Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { int i; if ( argc == 1 ) { cout << "Enter at least one file name\n"; return -1; } for (i = 1; i < argc; i++) { readLogFile(argv[i]); } return 0; }
Program.exe Filename1.txt Filename2.txt ...the files will get processed. I hope that answers your question.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
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...
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...
•
•
•
•
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...
バルサミコ酢やっぱいらへんで
•
•
•
•
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.
C Syntax (Toggle Plain Text)
int main(int ac, char *av[]) { int parm=1; while (parn < ac) { puts(av[parm++]); } return 0; }
By the way, using scanf() to read strings is just like using gets() -- you should really us something safer like fgets().
•
•
•
•
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:
C Syntax (Toggle Plain Text)
int main(int ac, char *av[]) { int parm=1; while (parn < ac) { puts(av[parm++]); } return 0; }
while (parn < ac)Edit:
Works in Cygwin. That is a consolation.
Last edited by WolfPack; Aug 2nd, 2006 at 3:02 pm.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
Hi All,
This is the code to read the text file:
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;
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..
This is the code to read the text file:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <mysql.h> #define HOST "localhost" #define USER "yati" #define PASSWD "yati" #define DB_NAME "subscriberTool" void readLogFile(const char* filename); void insert2DB(); void logSeparator(char log[1600]); void logSeparatorDetails(char logDetails[2500]); void date2DB(char dateLog[20]); static MYSQL demo_db; char string1[150], msg[250]; char date[50],time[50],hpno[50]; char dateLog[20], filename[200]; char dd[20], mm[20], yyyy[20], theDate[20]; int main(int argc, char *argv[]) { int i; if ( argc == 1 ) { return -1; } for (i = 1; i < argc; i++) { readLogFile(argv[i]); } return 0; } void readLogFile(const char* filename) { char line[1600]; FILE *fp; fp=fopen(filename, "r+"); if(fp==NULL) { printf("file not found!\n"); exit(0); } else { while(!feof(fp)) { bzero(line, sizeof(line)); fgets(line,1600,fp); logSeparator(line); logSeparatorDetails(string1); date2DB(date); insert2DB(); } } fclose(fp); return; } void insert2DB() { char query[16384]; char query1[16384]; int stat; if(!mysql_connect(&demo_db, HOST, USER, PASSWD)) { printf(mysql_error(&demo_db)); exit(1); } if(mysql_select_db(&demo_db, DB_NAME)) { printf(mysql_error(&demo_db)); exit(1); } bzero(query, sizeof(query1)); sprintf(query, "INSERT INTO PostpaidProfile2 (MobileNumber,PackagePlan,Status) VALUES('%s','%s',1)",hpno,msg,stat); printf("%s\n",query); if(mysql_real_query(&demo_db, query, strlen(query)+255)) { printf(mysql_error(&demo_db)); bzero(query, sizeof(query)); exit(1); } mysql_close(&demo_db); return; } void logSeparator(char log[2000]) { char *temp, msgTemp[200]; int io; io=0; bzero(string1, sizeof(string1)); bzero(msg, sizeof(msg)); temp = strtok (log," "); while (temp != NULL) { io++; switch(io) { case 1: strcpy (string1, temp); break; default: strcpy (msgTemp, temp); if (strlen(msg) != 0) { strcat(msg," "); } strcat(msg,msgTemp); break; } temp = strtok (NULL," "); } } void logSeparatorDetails(char logDetails[2000]) { char *temp2; int io2; io2=0; bzero(hpno, sizeof(hpno)); bzero(msg, sizeof(msg)); temp2 = strtok (logDetails,","); while (temp2 != NULL) { io2++; switch(io2) { case 1: strcpy (hpno, temp2); break; case 2: strcpy (msg, temp2); break; } temp2 = strtok (NULL,","); } } void date2DB(char dateLog[20]) { bzero(dd, sizeof(dd)); bzero(mm, sizeof(mm)); bzero(yyyy, sizeof(yyyy)); bzero(theDate, sizeof(theDate)); strncpy(dd,dateLog,2); strncpy(mm,dateLog+2,2); strncpy(yyyy,dateLog+4,4); strcat(theDate,yyyy); strcat(theDate,"-"); strcat(theDate,mm); strcat(theDate,"-"); strcat(theDate,dd); //convert to yyyy-mm-dd as in mysql date format return; }
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;
C Syntax (Toggle Plain Text)
+---------------+------------------+------+-----+---------+----------------+ | 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 | | +---------------+------------------+------+-----+---------+----------------+
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.
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
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:
Thanks in advance
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:
C Syntax (Toggle Plain Text)
void writeLogFile(char line2[1600]) { FILE *fp; fp=fopen("internet_access_lookup_table.txt", "a"); if(fp==NULL) { printf("file not found!\n"); exit(0); } else { if ( (fputs(line2, fp)) == EOF) { printf("error write..."); } if ( (fputs("\n", fp)) == EOF) { printf("error write1..."); } } fclose(fp); }
Thanks in advance
![]() |
Similar Threads
- Read text file to a certain point (C#)
- read text file (C#)
- using a "for" loop to read a text file (VB.NET)
- Read in a string from a text file (C)
- how do i read the last line of a text file? (Python)
Other Threads in the C Forum
- Previous Thread: reading a line in excel
- Next Thread: sum of the linked list
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide infiniteloop initialization interest intmain() iso keyboard kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h






