Thread: C program to copy log file frm database

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    C program to copy log file frm database

    Hi folkz,

    Need help in writing a C program..The scenario is as follows:

    We need to copy a log file frm the database to a txt file and the txt file should automatically get updated whenever the log file is modified... Also there shld be a facility to filter data frm the txt file based on a condition...

    Cud anyone help me out with diz..

    Thnx in advance.

    Nirmala.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Depending on how your DB writes it's log file, you can do both of those things with a couple of simple linux commands

    1. tail -f db.log > copy.txt
    2. grep "regex condition" copy.txt

  3. #3
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    it`s easy to take the file from the DB and copy it to a text file ,
    but the big problem how it will be updated automatically ?!!!

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >but the big problem how it will be updated automatically ?!!!

    tail -f will follow the log, as things are written to it tail will send the changes, through the redirect, to the copy.

  5. #5
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    tail is a unix command ,,

    i want to do it in C not in Linux Command ,

  6. #6
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    is my question hard to answer ??!!

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    First off, don't bump up your threads.

    Second off, did you ever just think of taking Perspective's answer and just doing system() with those (that is, if you're using linux) ?

    It's system dependant, I know, but I figure you won't really ber ditributing your program, now will you ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i want to do it in C not in Linux Command
    So think about what tail needs to do (or find the source code.

    My guess is something like
    Code:
    while ( 1 ) {
      while ( fgets( buff, sizeof buff, fp ) != NULL ) {
      }
      // got to EOF, now for magic
      clearerr( fp );
      sleep( 1 );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    Salem , yes this how it`s done but !
    you have to let the program work in the Background .. ^_^
    with & symbol in Unix machine .

    gcc p.c
    will produce
    a.out
    to make it work in background
    Code:
    &a.out

  10. #10
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    is there a way guys to make the program work in the background without using & symbol ?!!

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Whether or not a program runs in the background is up to the user/program that starts it. Why not wrap your program in a script.

    echo "./a.out &" > run.sh
    chmod ug+x run.sh

    The run your script instead of the app directly.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is there a way guys to make the program work in the background without using & symbol
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Quote Originally Posted by Meshal
    is there a way guys to make the program work in the background without using & symbol ?!!
    fork()-execve().

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Copying works perfectly fine but updation is still a big question mark !! How do i go abt it !! And also incase i want to select records frm the text file based on a condition, how do i do it in C ???

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Any better for you?

    Code:
    if ( fork() == 0 ) {
      // Yay, we detached ourselves
      while ( 1 ) {
        while ( fgets( buff, sizeof buff, fp ) != NULL ) {
          // do stuff with the line
        }
        // got to EOF, now for magic
        clearerr( fp );
        sleep( 1 );
      }
    } else {
      // this is the parent, which can now just exit
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 12:31 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM