| | |
read text file
![]() |
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2006
Posts: 56
Reputation:
Solved Threads: 3
•
•
•
•
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
C Syntax (Toggle Plain Text)
// reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; //this will contain the data read from the file ifstream myfile ("example.txt"); //opening the file. if (myfile.is_open()) //if the file is open { while (! myfile.eof() ) //while the end of file is NOT reached { getline (myfile,line); //get one line from the file cout << line << endl; //and output it } myfile.close(); //closing the file } else cout << "Unable to open file"; //if the file is not open output <-- return 0; }
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.
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
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:
Thanks
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <mysql.h> #define HOST "localhost" #define USER "" #define PASSWD "" #define DB_NAME "" void readLogFile(); void insert2DB(); void logSeparator(char log[1600]); void logSeparatorDetails(char logDetails[2500]); void date2DB(char dateLog[20]); //int operatorPrefix(); static MYSQL demo_db; char string1[150], msg[250]; char date[50],time[50],noXXX[50],hpno[50],noYYY[50]; char dateLog[20]; char dd[20], mm[20], yyyy[20], theDate[20]; int main() { readLogFile(); } void readLogFile() { char line[1600]; FILE *fp; fp=fopen("DB1-12-27-07-2006.txt", "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); //operatorPrefix(); 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; }
Thanks
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. C Syntax (Toggle Plain Text)
readLogFile("file1.txt"); readLogFile("file2.txt");
バルサミコ酢やっぱいらへんで
![]() |
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 |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic fflush fgets file floatingpointvalidation forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o inches include infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation owf pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windowsapi






