DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   read text file (http://www.daniweb.com/forums/thread51383.html)

whitemoss Jul 31st, 2006 3:37 am
read text file
 
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

Eddy Dean Jul 31st, 2006 3:41 am
Re: read text file
 
Quote:

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

// 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 ;)

whitemoss Jul 31st, 2006 3:48 am
Re: read text file
 
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

Eddy Dean Jul 31st, 2006 3:51 am
Re: read text file
 
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

whitemoss Jul 31st, 2006 6:44 am
Re: read text file
 
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

iamthwee Jul 31st, 2006 7:27 am
Re: read text file
 
http://www.daniweb.com/code/snippet151.html

Ancient Dragon Jul 31st, 2006 8:12 am
Re: read text file
 
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.

whitemoss Aug 2nd, 2006 1:25 am
Re: read text file
 
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:
#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

WolfPack Aug 2nd, 2006 1:34 am
Re: read text file
 
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.
 readLogFile("file1.txt");
readLogFile("file2.txt");

whitemoss Aug 2nd, 2006 2:03 am
Re: read text file
 
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


All times are GMT -4. The time now is 1:39 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC