Thread: Read a file into a loop program

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    Read a file into a loop program

    I'm new to C and have a question. I have a master time clock, with the attached C programm running, that controls bells on a shop floor for purposes of employee start time, lunch break and end of day. I have hardcoded the time values in my C program and want to convert them to an external file to be read in to the program. This program is a continuous loop which monitors the time on the clock and when there is a match of any hardcoded value, it triggers a relay and sounds the shop bells. I have seen many examples of how to read an external file but not sure how read a file in a loop program. Do I read the file one record at a time or all records at one time? Not sure. I would appreciate any help or examples.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Read a file into a loop program

    Originally posted by TimeClock
    1) I have seen many examples of how to read an external file but not sure how read a file in a loop program.
    2) Do I read the file one record at a time or all records at one time?
    1) The same way
    2) Either.

    Before you enter your loop, read a file to populate the variables that replace the hardcoded values, then close the file.

    Once a day (mignight?), reread the file just in case the file has been updated with new values.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    Thumbs up

    Thank you both Salem and Waltp for your responce. You've been very helpful. I've been out of town for a couple of weeks and just getting back to this program. Waltp, do you have an example of how I can read a text file and load each TOD value into a variable? I need to load the time in my file into each "alarm" variable is suppose. Thanks.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> how I can read a text file and load each TOD value into a variable?<<
    fgets()
    sscanf()
    http://www.rt.com/man
    Have a go, and post again when you get stuck.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by TimeClock
    Thank you both Salem and Waltp for your responce. You've been very helpful. I've been out of town for a couple of weeks and just getting back to this program. Waltp, do you have an example of how I can read a text file and load each TOD value into a variable? I need to load the time in my file into each "alarm" variable is suppose. Thanks.
    Sure, but you'll learn more if you try it yourself and if you get stuck, ask for help.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void readfromfileinaloop( FILE*fp )
    {
        int c;
        if( (c=fgetc(fp)) == EOF )
        {
            printf("\nEnd of File\n");
            fflush(stdout);
            return;
        }
        printf("%c",c);
        readfromfileinaloop(fp);
    }

    Or did you want something useful?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple File Read Program...
    By PowerHouse in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 11:30 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Can I write & read file in same program?
    By chad03 in forum C Programming
    Replies: 2
    Last Post: 08-04-2003, 08:39 AM