Thread: Idea to enhance this code to hit the database and check the file size

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    9

    Post Idea to enhance this code to hit the database and check the file size

    Hi All,

    I have been given a task of checking the size of file in Linux machine using C Program.

    Below is what I came up with

    Code:
    #include <stdio.h>
    void main(int argc, char **argv)
    {
     FILE *fp;
       char ch;
     int size = 0;
     fp = fopen(argv[1], "r");
     if (fp == NULL)
        printf("\n Failure ");
        else 
           printf("\success ");
        fseek(fp, 0, 2);    
        size = ftell(fp);   
        printf("The size of given file is : %d\n", size);    
        fclose(fp);
    }

    But Now I have been asked to modify this code in a way that instead of passing the file name as parameter, I have to make a database connection and fetch the value of the filename and location from the database table and check the size . My program has to repeat this process every ten minutes, which means every ten minutes my program has to hit the database, fetch the value and check the size in the file system.



    I heard like I Have to create a Fork Call, and have the child instance run every ten minutes.

    I am new to C Programming, I could only come up with above code.
    Please give an example of how to achieve my requirement.

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    How come you have been given the task to do this, at a company, if you don't know how? And why do you have to do it in C, if you don't know how to use C?

    Or is this some sort of course assignment?

    EDIT: The easiest way would probably be to create a simple program that access the database, find the file name, use stat(2) to check the size and then you use cron to run the program every 10 minutes.
    Last edited by Jimmy; 01-13-2015 at 03:07 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Till you learn how to write and call functions this assigment is way way over your ability to do.

    Functions in C - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    2 is also not (necessarily) a valid value for the third argument of fseek(). It does also, however, provide evidence you copied the code from somewhere else - a beginner would not know enough to pick such a value.

    It might pay to look at this site's homework policy, and realise that people here can often tell when a beginner is posting code they have not written on their own.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    9
    Hi All

    Thanks for the response. As u said in new to c program (new in the sense barely worked on it in production ) but I'm not a beginner to programming ... I am in java projects for two years from now.. As Jimmy said it could be done in shell script ,it wont be configurable and flexible as a c program .

    I could do it in java with ease but the problem is we don't have jvm installed and there is also a specific reason for it...

    I didn't ask anyone to do homework for me ...I just asked an example like an idea or a template Since I already have a hectic schedule on an another java online project I needed some help on this because of timing constraints ...

    I don't know much about postings so may be I got misinterpreted...
    Its completely your wish whether you could help or deny ... But please don't assume things like how I got this requirement..

    In my place , things are like this only ... Suddenly java guy will be asked to do In c or any other languages ..

    I will do it myself and post the output shortly it ll take extra time that's all ...

    Thanks

  6. #6
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Well, it is hard to be more specific as you do not say what kind of database it is, MySQL maybe?

    And btw, I said nothing about using a shell script! :-)

    I still think the easiest way to go about this is to write a small program that just connects to the data base to get the file name of the file you want and then you just use stat(2) to see the size and then just run it from cron, as cron is probably running on your machine if it runs Linux and cron is sort of made for running things periodically.

    That way you don't have to daemonize and fiddle around with stuff like that. I don't think you have to use fork for something like this, if the only thing you want to do is calculate the size of one file every 10 minutes. Or maybe the program is supposed to do more that you are not telling us?

    And connecting to a database and just reading once should probably be a piece of cake if you know what database you are using.

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    9
    Hi Jimmy,

    Thanks for the response. I will try as you have advised without Fork. The Database Im using is MS Sql Server. I specificaly told to use Fork, thats y I asked.


    Quote Originally Posted by Jimmy View Post
    maybe the program is supposed to do more that you are not telling us?
    Nothing Secret Jimmy, just want to check the file size and if it exceeds, take a backup or delete as per current situation. no other process involved.

    It is just a minor enhancement to improve the disk space as the file is growing large.

    Thanks Again !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check whether file exists and exceeds a certain size
    By strokebow in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2012, 12:30 PM
  2. Source-code file size? [Poll]
    By Devil Panther in forum General Discussions
    Replies: 13
    Last Post: 09-18-2009, 02:47 PM
  3. Ansi C - check file size in bytes.
    By Ironic in forum C Programming
    Replies: 9
    Last Post: 04-02-2009, 11:54 AM
  4. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

Tags for this Thread